我正在尝试使用 Python 修改 Excel 文件,但我无法让 xlutils
包正常工作。当我尝试一个例子时(来自 this 线程):
from xlutils.copy import copy
w = copy('book1.xls')
w.get_sheet(0).write(0,0,"foo")
w.save('book2.xls')
我得到以下结果:
Traceback (most recent call last):
File "names3.py", line 2, in <module>
w = copy('names.xls')
File "C:\Program Files (x86)\Python27\lib\site-package
g\xlutils\copy.py", line 19, in copy
w
File "C:\Program Files (x86)\Python27\lib\site-package
g\xlutils\filter.py", line 937, in process
reader(chain[0])
File "C:\Program Files (x86)\Python27\lib\site-package
g\xlutils\filter.py", line 61, in __call__
filter.workbook(workbook,filename)
File "C:\Program Files (x86)\Python27\lib\site-package
g\xlutils\filter.py", line 287, in workbook
self.wtbook.dates_1904 = rdbook.datemode
AttributeError: 'str' object has no attribute 'datemode'
我几乎找不到有关此错误的任何信息,非常感谢您的帮助!
谢谢
最佳答案
xlutils.copy
适用于 xlrd.Book
实例。您需要先创建这样的实例。这有效:
from xlrd import open_workbook
from xlutils.copy import copy
wb = open_workbook('book1.xls')
wb_copy = copy(wb)
wb_copy.get_sheet(0).write(0,0,"foo")
wb_copy.save('book2.xls')
关于python - 使用 xlutils 的日期模式错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26339834/