Hi I'm using python to parse through an excel file and copy only the contents I want to another excel file however, I want the new cells formatted in a different way. Currently I'm using the xlrd and xlwt libraries. I looked at the documentation on their website and it doesn't seem to address it, only font formatting such as bolding and underlining. Specifically I want my new copied cells centered and some of the other cells merged. Any ideas?
-
check out this link: youlikeprogramming.com/2011/04/…Brad– Brad2013-11-14 19:54:04 +00:00Commented Nov 14, 2013 at 19:54
-
Take a look at openpyxl if xlsx format files are ok for your use. I have found it to be much more friendly than xlrd/xlwt. pythonhosted.org/openpyxl and openpyxl.readthedocs.org/en/latestDave_750– Dave_7502013-11-14 22:19:19 +00:00Commented Nov 14, 2013 at 22:19
2 Answers
In the comments, Joe points out that formatting is supported by xlrd and xlwt. However, you also can use win32com to drive excel directly.
See: Using Automation Objects from Python
Once you have this working you can do pretty much anything excel can do. Unfortunately there isn't complete documentation. The best bet is to read the excel VB documentation (for the version you have) and guess at the python binding.
If you know the general format, it's easiest to start with an excel file that has all the formatting and formulas predefined.
4 Comments
update: I have found the solution by importing easyxf from xlwt
I set the styles by the statement:
style = easyxf('align:horizontal center;')
so when i write to the sheet i can use:
sheet.write(row, col, item_to_write, style)
Documentation from: http://www.simplistix.co.uk/presentations/python-excel.pdf