我正在尝试单击按钮下载Excel文件。但是我面临错误
IOERROR : [ERRNO13] Permission Denied on "Test.xlsx"
这就是我想要做的。
<button name="fetch_invoice" attrs="{'invisible':[('show_transaction_specific_fields', '=', False)]}" type="object" class="btn btn-primary btn-sm o_list_button_add" string="Download Invoice"/>
@api.multi
def fetch_invoice(self):
attachment_id, file_name = self.create_excel_file()
base_url = self.env['ir.config_parameter'].get_param('amgl.live.url')
return {
'type': 'ir.actions.act_url',
'url': base_url + '/web/content/%s/%s'%(attachment_id, file_name),
'target': 'self'
}
def create_excel_file(self):
file_name = 'Test.xlsx'
workbook = xlsxwriter.Workbook(file_name)
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello world')
workbook.close()
attachment = self.add_file_in_attachment(file_name)
attachment_id = attachment.id
return attachment_id, file_name
def add_file_in_attachment(self, file_name):
byte_data = 0
with open(file_name, "rb") as xlfile:
byte_data = xlfile.read()
attachment = self.env['ir.attachment'].create({'name': file_name,
'datas': base64.b64encode(byte_data),
'datas_fname': file_name,
'res_model': 'res.users',
'res_id': 1, }
现在,当我单击按钮并尝试获取excel报告时,它给了我以下错误
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/odoo/http.py", line 638, in
_handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python2.7/dist-packages/odoo/http.py", line 675, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python2.7/dist-packages/odoo/http.py", line 331, in
_call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/odoo/service/model.py", line 101, in
wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/odoo/http.py", line 324, in
checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python2.7/dist-packages/odoo/http.py", line 933, in __call__
return self.method(*args, **kw)
File "/usr/lib/python2.7/dist-packages/odoo/http.py", line 504, in
response_wrap
response = f(*args, **kw)
File "/usr/lib/python2.7/dist-
packages/odoo/addons/web/controllers/main.py",
line 889, in call_button
action = self._call_kw(model, method, args, {})
File "/usr/lib/python2.7/dist-packages/odoo/addons/web/controllers/main.py",
line 877, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/usr/lib/python2.7/dist-packages/odoo/api.py", line 681, in call_kw
return call_kw_multi(method, model, args, kwargs)
File "/usr/lib/python2.7/dist-packages/odoo/api.py", line 672, in
call_kw_multi
result = method(recs, *args, **kwargs)
File "/home/allshore/AMARK/amgl/models/reports.py", line 73, in
fetch_transaction_invoice
attachment_id, file_name = self.create_excel_file(start_date, end_date)
File "/home/allshore/AMARK/amgl/models/reports.py", line 404, in
create_excel_file
book.save(file_name)
File "/usr/local/lib/python2.7/dist-packages/openpyxl/workbook/workbook.py",
line 367, in save
save_workbook(self, filename)
File "/usr/local/lib/python2.7/dist-packages/openpyxl/writer/excel.py", line
282, in save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "/usr/lib/python2.7/zipfile.py", line 756, in __init__
self.fp = open(file, modeDict[mode])
IOError: [Errno 13] Permission denied: 'Test.xlsx'
答案 0 :(得分:1)
如果您没有权限,则尝试在目录中创建文件。向该目录添加了写权限。 使用 sudo chmod + w