Good day.
I know how to create excel and parse data to it using report_xlsx.
but my client has it's own excel templates with design and many worksheet.
I save their template in static/src/template/PAR_MOD_TEMPLATE.xlsx
so we decided to re-use their existing templates. Is it possible in Odoo?
I used openpyxl.
import openpyxl
def action_download_par_template(self):
content_lst = []
proj_task = self.env['project.task'].search([('project_id', '=', self.id)])
for task in proj_task:
vals = {
'task_id': task.id,
'task_name': task.name if task.name else '',
'site_id': task.site_id if task.site_id else '',
'site_name': task.site_name if task.site_name else '',
'area': task.area if task.area else '',
'region': task.region if task.region else '',
'province_district': task.province_district if task.province_district else '',
'municipality_city': task.municipality_city if task.municipality_city else '',
'first_nap': task.nap_1_8_actual if task.nap_1_8_actual else '',
'second_nap': task.nap_2_1_8_actual if task.nap_2_1_8_actual else '',
'total_lines': task.total_liones if task.total_liones else '',
'line_type': 'FTTH',
'remarks': task.remarks if task.remarks else '',
}
content_lst.append(vals)
file_path = get_module_resource('my_module', 'static/src/template', 'PAR_MOD_TEMPLATE.xlsx')
print("file_path>", file_path)
wb = openpyxl.load_workbook(file_path)
ws = wb['ODN_FTTH;MDU']
# ws.delete_rows(7, ws.max_row + 1)
while ws.max_row > 7:
# this method removes the row 8
ws.delete_rows(8)
ws_odn = wb['ODN PERMIT']
# ws_odn.delete_rows(6, ws_odn.max_row + 1)
while ws.max_row > 6:
# this method removes the row 8
ws.delete_rows(7)
row = 7
odn_row = 6
for lst in content_lst:
ws.cell(row=row, column=1).value = ws_odn.cell(row=odn_row, column=1).value = lst['task_name']
ws.cell(row=row, column=2).value = ws_odn.cell(row=odn_row, column=2).value = lst['site_id']
ws.cell(row=row, column=3).value = ws_odn.cell(row=odn_row, column=3).value = lst['site_name']
ws.cell(row=row, column=4).value = ws_odn.cell(row=odn_row, column=5).value = lst['area']
ws.cell(row=row, column=5).value = ws_odn.cell(row=odn_row, column=6).value = lst['region']
ws.cell(row=row, column=6).value = ws_odn.cell(row=odn_row, column=7).value = lst['province_district']
ws.cell(row=row, column=7).value = ws_odn.cell(row=odn_row, column=8).value = lst['municipality_city']
ws.cell(row=row, column=8).value = ws_odn.cell(row=odn_row, column=9).value = lst['first_nap']
ws.cell(row=row, column=9).value = lst['second_nap']
ws.cell(row=row, column=10).value = lst['total_lines']
ws.cell(row=row, column=11).value = ws_odn.cell(row=odn_row, column=10).value = lst['line_type']
ws.cell(row=row, column=15).value = ws_odn.cell(row=odn_row, column=14).value = lst['remarks']
row += 1
odn_row += 1
# ws.write(row, col, lst['task_id'])
# ws.write(row, col + 1, lst['site_id'])
# ws.write(row, col + 2, lst['site_name'])
# ws = row + 1
wb.save(file_path)
return {
'type': 'ir.actions.act_url',
'url': '/my_module/static/src/template/PAR_MOD_TEMPLATE.xlsx',
'target': 'new',
}
I download successfully the excel file.
But when I open the downloaded file it gives me an error like this
have you found a solution to this issue?