這是我的代碼。當我嘗試用評論保存xlxs時,它失敗了。我怎麼知道什麼時候再保存。使用openpyxl保存xlxs文件兩次,使用註釋成績
from openpyxl import load_workbook
import datetime
filename = u"large_table.xlsx"
model = load_workbook(filename)
model.properties.lastPrinted = datetime.datetime.now()
model.save(filename)
model.properties.lastPrinted = datetime.datetime.now()
model.save(filename)
回溯:看來self.workbook.vba_archive
設置爲無意外。
Traceback (most recent call last):
File "D:/h32workspace/trunk/event_editor/eric6/model/test_file.py", line 31, in <module>
model.save(filename)
File "C:\Python27\lib\site-packages\openpyxl\workbook\workbook.py", line 342, in save
save_workbook(self, filename)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 269, in save_workbook
writer.save(filename)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 251, in save
self.write_data()
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 81, in write_data
self._write_worksheets()
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 214, in _write_worksheets
self._write_comment(ws)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 184, in _write_comment
vml = fromstring(self.workbook.vba_archive.read(ws.legacy_drawing))
AttributeError: 'NoneType' object has no attribute 'read'
我試圖用keep_vba=True
加載工作簿,但如果未能正確保存文件。保存的文件無法打開。
嘗試使用XlsxWriter:https://pypi.python.org/pypi/XlsxWriter –
從回溯中可以清楚地看到,發佈的代碼不正確:您正在使用XLSM文件,但這些文件並未完全由標準,因爲所有宏觀相關的東西都是黑盒子。 –
@CharlieClark我發現Excel的評論將無法保存兩次 – Sam