How would i define a column width for the entire 'worksheet' or for each column when using write_row()
?
For example i have:
workbook = xlsxwriter.Workbook(loc_path + 'monbatch_test1.xlsx')
worksheet = workbook.add_worksheet()
monbatch = [
[a,b,c,],
[a,b,c,],
[a,b,c,]
]
row, col = 3, 0
for mon in monbatch:
worksheet.write_row(row, col, mon, rows_format)
row += 1
workbook.close()
I would like to change the column width for all columns (a,b,c)..
The with of 1 unit of the xlsxwriter columns is about equal to the width of one character. So, you can simulate autofit by setting each column to the max number of characters in that column.
If you are working with large files or are particularly concerned about speed then you may find XlsxWriter a better choice than OpenPyXL. XlsxWriter is a Python module that can be used to write text, numbers, formulas and hyperlinks to multiple worksheets in an Excel 2007+ XLSX file.
Setting the Column Width Set the width of a column by calling the Cells collection's setColumnWidth method. The setColumnWidth method takes the following parameters: Column index, the index of the column that you're changing the width of. Column width, the desired column width.
The easiest way to auto-size the width and height of a column is to call the Worksheet class' autoFitColumn method. The autoFitColumn method takes the column index (of the column about to be resized) as a parameter. Copy def autofit_column(self): \# Instantiating a Workbook object by excel file path workbook = self.
There is a relevant set_column()
method that accept width
:
set_column(first_col, last_col, width, cell_format, options)
Set properties for one or more columns of cells.
Here is how you can apply it:
worksheet.set_column(0, 2, 100)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With