Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openpyxl after load and save list index out of range(python)

I use openpyxl to work with excel documents in python. After opening and saving a file I cant open it in a file manager, it's broken.

workbook = openpyxl.load_workbook(path_to_tmp_xlsm)
workbook.save(path_to_tmp_xlsm)
print(path_to_tmp_xlsm)

my error:

list index out of range

ps This script works fine in Windows, but under Linux I have this error

Traceback (most recent call last):
  File "/home/alex/projects/bokapi/Main.py", line 11, in <module>
    class Main:
  File "/home/alex/projects/bokapi/Main.py", line 12, in Main
    EmailsDriveWorker()
  File "/home/alex/projects/bokapi/app/EmailDriveWorker.py", line 33, in __init__
    self.log_manager.save_all_companies_inf_log_to_gd()
  File "/home/alex/projects/bokapi/app/EmailDriveWorker.py", line 202, in save_all_companies_inf_log_to_gd
    path_to_xlsx = self.xmsl_box.update_log_file(log_list=logs, file=self.file)
  File "/home/alex/projects/bokapi/xmsl/XmslBox.py", line 172, in update_log_file
    workbook.save(path_to_tmp_xlsm)
  File "/home/alex/projects/bokapi/venv/lib/python3.5/site-packages/openpyxl/workbook/workbook.py", line 367, in save
    save_workbook(self, filename)
  File "/home/alex/projects/bokapi/venv/lib/python3.5/site-packages/openpyxl/writer/excel.py", line 284, in save_workbook
    writer.save(filename)
  File "/home/alex/projects/bokapi/venv/lib/python3.5/site-packages/openpyxl/writer/excel.py", line 266, in save
    self.write_data()
  File "/home/alex/projects/bokapi/venv/lib/python3.5/site-packages/openpyxl/writer/excel.py", line 95, in write_data
    archive.writestr(ARC_WORKBOOK, write_workbook(self.workbook))
  File "/home/alex/projects/bokapi/venv/lib/python3.5/site-packages/openpyxl/writer/workbook.py", line 95, in write_workbook
    wb.views[0].activeTab = active
IndexError: list index out of range

Process finished with exit code 1
like image 433
Алексей Нестерчук Avatar asked Sep 14 '25 23:09

Алексей Нестерчук


1 Answers

You (and I) seem to have run into this bug: Saving workbook gives IndexError on wb.views. (Also reported here, but that one was closed because the OP couldn't reproduce.) The good news is that this bug was fixed... two weeks ago. I don't know when it'll land in the distributed version of openpyxl, but in the meantime you can work around the bug like this:

wb = load_workbook("name.xlsx")
# Fix malformed workbook bug (gag)  
if not wb.views:
    wb.views.append(openpyxl.workbook.views.BookView())

I have verified that the error occurs when wb.views is an empty list. This just adds a new view with default values, and Excel seems to like it fine.

PS. It seems that some files just don't store a view (or if they do, openpyxl can't parse it.) In my case the same script, on the same computer (OS X), works on one doc and fails on another. Both were last edited with Excel, but the problem file was previously written with OpenOffice; perhaps that's relevant.

like image 143
alexis Avatar answered Sep 17 '25 12:09

alexis