I tried posting a bug report about this via heptapod, but for some reason they flagged my submission as spam and I am now blocked...
Workbooks with sheet scoped defined names in a sheet after a chart sheet are being saved incorrectly. They are currently being saved with their localSheetId attribute set to the index of the sheet from the worksheets list, but that doesn't take into account any chartsheets that might need to be accounted for.
The relevant code is in openpyxl/workbook/_writer.py on line 95.
for idx, sheet in enumerate(self.wb.worksheets):
Instead, it needs to loop through the sheets and get the index of each sheet from the sheetnames list.
for sheet in self.wb.worksheets:
idx = self.wb.sheetnames.index(sheet.title)
To replicate the error, I've attached a simple excel workbook which has a chartsheet for the first sheet and a second sheet with a defined range. Running the following and attempting to open the excel file will cause an error.
import openpyxl
wb = openpyxl.load_workbook("test.xlsx")
wb.save("test2.xlsx")
Any chance somebody could make that simple change?