Using Openpyxl to generate excel report

Copper Contributor

Hello all, I am working with a group, and we are having a slight problem with openpyxl. We am trying to generate a report of which I can add multiple sheets to said report. There are calculations that happen in the main that are sent over with the template and report file to another class.

First the user will select a template, which was premade. After the selection is made the code will create an excel sheet with.

wb = Workbook()

wb.save(client_name + report_Name_Extension)

This new sheet is the output or report file. The user will then be prompted to choose their desired output file, which could be the file recently created or another from another point in time. Once the user clicks on the button to create the report, all the information will be sent to the class mentioned before. In that class we use the copy_sheet, copy_sheet_attributes , and copy_cells methods from openpyxl and 3 other methods that use openpyxl to put the information into the excel sheet. We are able to create the first sheet, copy the template, and put in the information. In the code the file then saves and closes itself.

The problem comes when in the same session, we try to add another sheet of information to the same file, but we run into this error

File "C: … AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in__call__ return self.func(*args)

File "C: … \Main.py", line 1877, in <lambda> command=lambda: GenTRIP())

File "C: ...\Main.py", line 174, in GenTRIP ReportGenerator.GenTrip(reportFile, templateFile, Circuit_in_TRIP, SS_Client_in_TRIP, V_TRIP_in,

File "C: … \ReportGenerator.py", line 626, in __init__

create_Report_TRIP(reportFile, templateFile)

File "C: … \ReportGenerator.py", line 439, in create_Report_TRIP

report = openpyxl.load_workbook(report_File)

File "C: … \venv\lib\site-packages\openpyxl\reader\excel.py", line 317, in load_workbook reader.read()

File "C: … \venv\lib\site-packages\openpyxl\reader\excel.py", line 282, in read self.read_worksheets()

File "C: … \venv\lib\site-packages\openpyxl\reader\excel.py", line 228, in read_worksheets

ws_parser.bind_all()

File "C: ... \lib\site-packages\openpyxl\worksheet\_reader.py", line 453, in bind_all self.bind_row_dimensions()

File "C: … venv\lib\site-packages\openpyxl\worksheet\_reader.py", line 431, in bind_row_dimensions

rd['s'] = self.ws.parent._cell_styles[key]

IndexError: list index out of range

We investigated the ready.py, and searched for other fixes, but they have not worked. If you know about or ran into this problem, please reach out.

1 Reply
post your python codes and excel files.
i will test on my side.