Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XlsxWriter set global font size

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?

like image 876
guy Avatar asked May 16 '17 02:05

guy


People also ask

How do I remove gridlines in Excel Xlsxwriter?

hide_gridlines() Set the option to hide gridlines on the screen and the printed page. Parameters: option (int) – Hide gridline options.

How do I color a cell in Excel Xlsxwriter?

Note: The set_font_color() method is used to set the color of the font in a cell. To set the color of a cell use the set_bg_color() and set_pattern() methods.


1 Answers

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

like image 195
Tiny.D Avatar answered Sep 26 '22 02:09

Tiny.D