使用xlutils的datemode错误

时间:2014-10-13 12:17:46

标签: python xlrd xlutils

我正在尝试使用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'

我几乎找不到有关此错误的任何信息,我真的很感激任何帮助! 感谢

1 个答案:

答案 0 :(得分:2)

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')