Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python xlsxwriter change all cell widths when using write_row

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)..

like image 856
jes516 Avatar asked Jan 07 '15 04:01

jes516


People also ask

How do I autofit in XlsxWriter?

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.

Which is better Openpyxl vs XlsxWriter?

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.

How do you change the width of a column in Excel using Python?

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.

How do you autofit column width in python?

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.


1 Answers

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)
like image 125
alecxe Avatar answered Oct 06 '22 01:10

alecxe