Formatting Cells In Excel With Python
Solution 1:
Using xlwt:
from xlwt import *
font0 = Font()
font0.bold = Falsestyle0= XFStyle()
style0.font = font0wb= Workbook()
ws0 = wb.add_sheet('0')
ws0.write(0, 0, 'myNormalText', style0)
font1 = Font()
font1.bold = Truestyle1= XFStyle()
style1.font = font1
ws0.write(0, 1, 'myBoldText', style1)
wb.save('format.xls')
Solution 2:
For using Python for Excel operations in general, I highly recommend checking out this site. There are three python modules that allow you to do pretty much anything you need: xlrd (reading), xlwt (writing), and xlutils (copy/modify/filter). On the site I mentioned, there is quite a bit of associated information including documentation and examples. In particular, you may be interested in this example. Good luck!
Solution 3:
For generic examples of Excel scripting from Python, this snippet is very handy. It doesn't specifically do the "change font to regular", but that's just range.Font.Bold = False
in a function otherwise very similar to the set_border
one in that snippet.
Solution 4:
Here is a brief introduction to using xlwt
and the complementary xlrd
(for reading .xls
files). However, the Reddit thread where I discovered that article has a huge number of useful bits of advice, including some cautionary notes and how to use the win32com
module to write Excel files better (see this comment, for example) - frankly, I think the code is easier to read/maintain. You can probably learn a lot more over at the pretty active python-excel group.
Post a Comment for "Formatting Cells In Excel With Python"