我想在Odoo 10中使用openpyxl生成Excel报告,但反复显示:
在线路或笔记本上打印Excel工作表时出现Odoo服务器错误
以下是Python代码:
@api.model
def create(self, vals):
if vals.get('gstr2_file'):
dirpath_gstr2 = os.environ.get('HOME') or os.getcwd()
abs_filepath_gstr2 = os.path.abspath(os.path.join(dirpath_gstr2, vals['filename_gstr2']))
with open(abs_filepath_gstr2, 'w') as file:
file.write(base64.b64decode(vals.get('gstr2_file')))
#vals['excel_file'] = False
res = super(BiExcel, self).create(vals)
return res
@api.multi
def generate_gstr2(self,vals):
for file in self:
dirpath_gstr2 = os.environ.get('HOME') or os.getcwd()
base_path_gstr2 = os.path.abspath(os.path.join(dirpath_gstr2, file.filename_gstr2))
if os.path.isfile(base_path_gstr2):
wb_gstr2 = openpyxl.load_workbook(base_path_gstr2,data_only=True)
sheet_name_gstr2 = wb_gstr2.get_sheet_names()
sheet_gstr2 = wb_gstr2.get_sheet_by_name(sheet_name_gstr2[0])
field_gstr2_obj = self.env['ir.model.fields']
excel_gstr2_heading = []
for excel_row_gstr2 in range(1, sheet_gstr2.max_row+1):
if not sheet_gstr2.cell(row=excel_row_gstr2, column=1).value:
break
excel_gstr2_data = {}
excel_gstr2_data_field = []
excel_column_gstr2 =1
for excel_column_gstr2 in range(1, sheet_gstr2.max_column+1):
if excel_row_gstr2 == 1:
heading_gstr2 = sheet_gstr2.cell(row=excel_row_gstr2, column=excel_column_gstr2).value
#raise UserError(_(str((heading_gstr2))))
domain_gstr2 = [('field_description','=',heading_gstr2),('model' ,'=', 'bi.gstr2')]
field_gstr2_id = field_gstr2_obj.search(domain_gstr2)
excel_gstr2_data_field.append(field_gstr2_id.name)
excel_gstr2_heading.append(heading_gstr2)
#raise UserError(_(str((excel_gstr2_data_field))))
else:
domain_gstr2 = [('field_description','=',excel_gstr2_heading[excel_column_gstr2-1]),('model' ,'=', 'bi.gstr2')]
field_gstr2_id = field_gstr2_obj.search(domain_gstr2)
cell_gstr2_value = sheet_gstr2.cell(row=excel_row_gstr2, column=excel_column_gstr2).value
#raise UserError(_(str((cell_gstr2_value))))
#if field_gstr2_id:
excel_gstr2_data[field_gstr2_id.name]=cell_gstr2_value
excel_gstr2_data['gstr2_id']=file.id
excel_column_gstr2 = excel_column_gstr2 + 1
if excel_gstr2_data:
#raise UserError(_(str((excel_gstr2_data))))
self.env['bi.gstr2'].create(excel_gstr2_data)