2

Is it possible to copy a macros enabled excel workbook? For example, I have:

from xlutils.copy import copy
from xlrd import open_workbook
from tempfile import TemporaryFile

book = open_workbook("book.xlsm")
book_copy = copy(book)
book_copy.save("bookcopy.xlsm")
book_copy.save(TemporaryFile())

However, when I then click on bookcopy.xlsm to open it, I get the following error: "Excel cannot open the file 'bookcopy.xlsm' because the file format or the file extesion is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

I don't get this error when I save is as "bookcopy.xls", but I need it to be .xlsm. Does anyone have an idea what the problem is?

user3204260
  • 115
  • 4
  • 11
  • 1
    xlwt does not support xlsm afaik (in fact it doesnt support anything but xls) ... why not `shutil.copyfile('book.xlsm','book_copy.xlsm')` – Joran Beasley Feb 05 '14 at 18:12
  • 1
    @JoranBeasley is right. I think this is a bit of an overstretched way of doing it, besides the fact that it doesn't work. Won't copying the file using `shutil` or the like work just as well? Or is there a reason why you want to use xlutils for this? – WGS Feb 05 '14 at 18:12
  • I needed to make a copy so I can edit the excel sheet with book_copy.write()- is there another way to edit a xlsm sheet with python? – user3204260 Feb 05 '14 at 18:14
  • you can use win32com stuff to talk directly to excel (assuming you are running windows... its a big pain though... what are you trying to accomplish? and why cant you accomplish it with xls?) – Joran Beasley Feb 05 '14 at 18:35
  • I am working on a linear integer optimization problem, and using python to call the solver. Excel is connected to the database, and presents all the variables that can be used. I then have a python program that reads the data from excel with xlrd and solves the problem. I need the solution to then appear back in the original excel file. – user3204260 Feb 05 '14 at 19:13

0 Answers0