I'm using xlrd
, xlutils.copy
, and xlwt
to open up a template file, copy it, fill it with new values, and save it.
However, there doesn't seem to be any easy way to preserve the formatting of the cells; it always gets blown away and set to blank. Is there any simple way I can do this?
Thanks! /YGA
A sample script:
from xlrd import open_workbook
from xlutils.copy import copy
rb = open_workbook('output_template.xls',formatting_info=True)
rs = rb.sheet_by_index(0)
wb = copy(rb)
ws = wb.get_sheet(0)
for i,cell in enumerate(rs.col(8)):
if not i:
continue
ws.write(i,2,22,plain)
wb.save('output.xls')
Versions:
xlrd
: 0.7.1xlwt
: 0.7.2
Here's an example of usage of code that I'll propose as a patch against xlutils 1.4.1
There are two parts to this.
First, you must enable the reading of formatting info when opening the source workbook. The copy operation will then copy the formatting over.
Secondly, you must deal with the fact that changing a cell value resets the formatting of that cell.
This is less pretty; I use the following hack where I manually copy the formatting index (
xf_idx
) over:This preserves almost all formatting. Cell comments are not copied, though.