Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change fontsize in excel using python

Tags:

python

excel

xlwt

I have to create a content with font as Times New Roman and font size as 16.How to create using python script ?

My sample script

import xlwt
workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.add_sheet('My Worksheet')
font = xlwt.Font() # Create the Font
font.name = 'Times New Roman'
style = xlwt.XFStyle() # Create the Style
style.font = font # Apply the Font to the Style
worksheet.write(0, 0, label = 'Unformatted value')
worksheet.write(1, 0, label = 'Formatted value') # Apply the Style to the Cell
workbook.save('fontxl.xls')
like image 297
gmanikandan Avatar asked May 20 '13 11:05

gmanikandan


People also ask

How do I change the font color in Excel using Python?

work_sheet_a1. font = Font(size=23, underline='single', color='FFBB00', bold=True, italic=True) #We apply the following parameters to the text: size - 23, underline, color = FFBB00 (text color is specified in RGB), bold, oblique. If we do not need a bold font, we use the construction: bold = False.

How do I bold text in Excel using Python?

How do you bold and italicize in Python? To make text bold and italic, you can enclose the text in the escape sequence '\033[1;3m' and '\033[0m'.


2 Answers

You set the font's height in "twips", which are 1/20 of a point:

font.height = 320 # 16 * 20, for 16 point
like image 174
Wooble Avatar answered Oct 21 '22 05:10

Wooble


Although the comment says that you do, you don't actually apply the style you defined!
Use the style keyword in the write() call:

worksheet.write(1, 0, label = 'Formatted value', style = style) # Apply the Style
like image 44
Junuxx Avatar answered Oct 21 '22 05:10

Junuxx