XlsxWriter set global font size

guy picture guy · May 16, 2017 · Viewed 12.2k times · Source

How do permanently set the font size using xlswriter when you first create the workbook?

I tried:

book = xlsxwriter.Workbook(os.getcwd() + '\\test.xlsx')
sheet1 = book.add_worksheet()
format = book.add_format()
format.set_font_size(10)

But I still get the default size 11 in the output. What is the issue?

Answer

Tiny.D picture Tiny.D · May 16, 2017

1, For single cell font size, you have to pass the format to the write like this:

workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
# Add a font size 10 format.
format = workbook.add_format()
format.set_font_size(10)
# Write some simple text.
worksheet.write('A1', 'Hello', format)

# Text with formatting.
worksheet.write('A2', 'World', bold)

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)


workbook.close()

Hello will be set to font size 10.

enter image description here

Update: 2, For all cells font size, you could set the default format of the workbook:

import xlsxwriter
workbook = xlsxwriter.Workbook('demo.xlsx')

# default cell format to size 10 
workbook.formats[0].set_font_size(10)

worksheet = workbook.add_worksheet()


# Write some simple text.
worksheet.write('A1', 'Hello')

worksheet.write('A2', 'World')

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)

workbook.close()

All cell will change to font size 10:

enter image description here