1

I have some code that opens an Excel workbook with xlrd, copies it with xlutils, updates it with xlwt and then writes it back out to another file.

The resulting workbook is missing all the named ranges that were present in the original. I'm 100% sure the named ranges are present in the original because I'm actually reading them (from the xlrd object, before the copy) and basing my updates on them.

Assuming that this is the natural effect of using xlutils.copy.copy, does xlwt have some way for me to recreate the named ranges that I read from the xlrd version of the same workbook? The documentation (at least, what I could find) is sparse.

Here is a python session that shows the problem:

>>> from xlrd import open_workbook
>>> wb = open_workbook('sc_auction_template.xls', formatting_info=True)
>>> print len(wb.name_obj_list) # see, there are named ranges
9
>>> from xlutils.copy import copy
>>> wb2 = copy(wb)
>>> wb2.save('test.xls')
>>> wb = open_workbook('test.xls')
>>> print len(wb.name_obj_list) # but now there are none!
0
Larry Lustig
  • 49,320
  • 14
  • 110
  • 160
  • What's "formatting_info=True"? You have put it in the first open, but not the second... Why? – Antoni4040 Oct 06 '12 at 18:04
  • It's necessary to to xlrd to read the cell formatting information from the spreadsheet. It's there because I cut this code and edited it down from actual running code. I don't think it's relevant to the named ranges issue (except that it demonstrates that I had to go to some trouble to even move the cell formats from spreadsheet to spreadsheet). – Larry Lustig Oct 07 '12 at 10:17

1 Answers1

0

For now, the answer is unfortunately no, xlwt doesn't support writing names. Maybe someday, but I don't expect it anytime soon.

As far as I know, the only way to programmatically write names in .xls files is to automate a running instance of an application that can, such as Excel itself on Windows with pywin32 or pywinauto or some flavor of Visual Basic, etc. I believe there are analogous ways to script OpenOffice.org if Excel is not available to you.

John Y
  • 14,123
  • 2
  • 48
  • 72