简体   繁体   中英

Openpyxl load_workbook Index Error only with a certain worksheet

I am trying to read data from a single worksheet in an Excel workbook (Macro Enabled). The workbook comes with several other worksheets which I don't need anything from, one of which is giving me trouble. I can open the workbook in Excel with no issues, but using load_workbook with openpyxl results in an "IndexError: list index out of range".

I did lots of testing and found that the IndexError is triggered only when the troublesome worksheet is the last worksheet tab in the workbook. If it is not the last worksheet in the workbook, I am able to use load_workbook with no issue. The troublesome worksheet itself contains an Excel print area. If I remove that print area, I am able to use load_workbook with no issue.

I need to programmatically read data from hundreds of workbooks with the same issue, so unfortunately I cannot just remove the bad worksheet in excel.

Is there anyone who is familiar with openpyxl and/or Excel at a lower level and knows what is happening here?

Alternatively, does anyone know a way to do some pre-processing on a workbook without having to load it with load_workbook. That way I can remove the bad worksheet before calling load_workbook.

Thanks

This is what I came across, I dont K how to answeryour question at that level but loading workbook wise, and formatting based offof color is what I searched up and this is the code explained, I will get back to the rest of your question later when I am near my computer.

# Combining dataFrames in excel
excelName = input("Label your excel file: ")
xlsxAUTO = '.xlsx'
excelAutoNamed = excelName + xlsxAUTO
writer = pd.ExcelWriter(excelAutoNamed,engine='xlsxwriter')

Wsheet = excelAutoNamed.worksheet[1]



describe_df.to_excel(writer,sheet_name='Validation',startrow=0 , startcol=0)

header = excelAutoNamed.add_format({'bold': True, 'bottom': 2, 'bg_color': '#F9DA04'})
Wsheet.set_row(0, None, header)





df.to_excel(writer,sheet_name='Validation',startrow=len(df.columns), startcol=0)
writer.save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM