Menachem
Menachem

Reputation: 297

How to fix alignment problem in xlsxwriter

I am using xlsxwriter to create Excel sheets using Python. Everything works fine. I need to change for all the rows the row height to 23 and the first line of the code below did it correctly. I also need to vertically align to the center the text in all the rows and could not make it work. Any suggestion how to make it right. I am using Python 3.6.

worksheet.set_default_row(23) # Set the row height for all the rows
format = workbook.add_format()
format.set_align('vcenter')

Upvotes: 4

Views: 22519

Answers (2)

jmcnamara
jmcnamara

Reputation: 41644

The best way to do this, and the way that Excel does it behind the scenes, is to set the row height with worksheet.set_default_row() and the formatting with worksheet.set_column().

Here is an example:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')

worksheet = workbook.add_worksheet()

# Set the default height of all the rows, efficiently.
worksheet.set_default_row(23)

# Format all the columns.
my_format = workbook.add_format()
my_format.set_align('vcenter')

worksheet.set_column('A:XFD', None, my_format)

workbook.close()

Note, any cell specific formatting added via the write() methods will overwrite the column format. Any data written without formats will pick up the column format.

Upvotes: 9

Rajith Thennakoon
Rajith Thennakoon

Reputation: 4130

According to the documentation here,you can add format to the cell and apply formats when write the excel file

cell_format = workbook.add_format()
cell_format.set_align('vcenter')
worksheet.write(<some params>, cell_format)

please refer documentation for more details

https://xlsxwriter.readthedocs.io/format.html

Upvotes: 4

Related Questions