9

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')
gmanikandan
  • 475
  • 3
  • 10
  • 16

4 Answers4

16

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

font.height = 320 # 16 * 20, for 16 point
Wooble
  • 87,717
  • 12
  • 108
  • 131
1

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
Junuxx
  • 14,011
  • 5
  • 41
  • 71
1

just add this

style = xlwt.easyxf('font: bold 1,height 280;')
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
Hamza Salem
  • 11
  • 1
  • 3
0

style0 = xlwt.easyxf('font: bold 1,height 260, name Times New Roman, color-index red')

You should be user like if you want font change and color also use may be it will be work on excel

  • 1
    Hi and welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and check the [help center](https://stackoverflow.com/editing-help) for info on how to format code. – Tyler2P Dec 14 '21 at 17:52