Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting cells in Excel with Python

How do I format cells in Excel with python?

In particular I need to change the font of several subsequent rows to be regular instead of bold.

like image 507
Sasha Avatar asked Jun 22 '09 21:06

Sasha


2 Answers

Using xlwt:

from xlwt import *

font0 = Font()
font0.bold = False

style0 = XFStyle()
style0.font = font0

wb = Workbook()
ws0 = wb.add_sheet('0')

ws0.write(0, 0, 'myNormalText', style0)

font1 = Font()
font1.bold = True

style1 = XFStyle()
style1.font = font1

ws0.write(0, 1, 'myBoldText', style1)

wb.save('format.xls')
like image 108
mechanical_meat Avatar answered Sep 30 '22 13:09

mechanical_meat


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!

like image 45
Brendan Wood Avatar answered Sep 30 '22 13:09

Brendan Wood