user3265339
user3265339

Reputation: 51

datemode error using xlutils

I am trying to modify Excel files using Python, but I can't get the xlutils package to work correctly. When I try an example (from this thread):

from xlutils.copy import copy
w = copy('book1.xls')
w.get_sheet(0).write(0,0,"foo")
w.save('book2.xls')

I get the following result:

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'

I can barely find any information about this error, I would really appreciate any help! Thanks

Upvotes: 5

Views: 3566

Answers (1)

Mike M&#252;ller
Mike M&#252;ller

Reputation: 85462

xlutils.copy works on a xlrd.Book instance. You need to create such an instance first. This works:

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

Upvotes: 2

Related Questions