I'm getting lost of using worksheet.set_column
all the time. Is there any possibility of setting the width of all columns automatically?
What would be the Python function that simulates it? (using only xlsxwriter
library):
def autofit(filename, worksheet_name):
# ???
Unfortunately xlsxwriter doesnt provide autofit option. You can however track the largest entry for each column and then set the column width in the end with set column command. In your case for instance, you should set the width of B column to the length of the largest string.
When you see that four-way arrow key, just double click on your mouse to auto adjust the excel column width, which is equal to the highest lengthy value in the column. When we double-click on the column right corner, it has adjusted the column width, which is equal to the value in the cell A1 which is 9.57 characters.
How to copy the column width in Excel (in the same or to another sheet) You already know how to make several or all columns on the sheet the same width by dragging the column border. If you have already resized one column the way you want, then you can simply copy that width to other columns.
When we type some data into the cell, that value is not necessarily within the default width of the column. When the cell value is more than the default character width, that cell’s respective column would not automatically change its width. For example, look at the below image.
Is there any possibility of setting the width of all columns automatically?
Unfortunately, not.
From the XlsxWriter FAQ:
Q. Is there an "AutoFit" option for columns?
Unfortunately, there is no way to specify "AutoFit" for a column in the Excel file format. This feature is only available at runtime from within Excel. It is possible to simulate "AutoFit" in your application by tracking the maximum width of the data in the column as your write it and then adjusting the column width at the end.
I only know of a way to do this with COM.
import contextlib, os, win32com.client
@contextlib.contextmanager
def load_xl_file(xlfilepath):
''' Open an existing Excel file using a context manager
`xlfilepath`: path to an existing Excel file '''
xl = win32com.client.DispatchEx("Excel.Application")
wb = xl.Workbooks.Open(xlfilepath)
try:
yield wb
finally:
wb.Close(SaveChanges=True)
xl.Quit()
xl = None # this actually ends the process
def xlautofit(xlfilepath,skip_first_col=False):
''' relies on win32com.client to autofit columns on data sheets
remember that this is using COM so sheet numbers start at 1 (not 0),
so to avoid requiring the caller to remember this, we increment
returns full path (including dir) to file '''
if os.path.splitext(xlfilepath)[1] not in ('.xls','.xlsx'):
raise
return -1
autofitbegcol = 1
if skip_first_col:
autofitbegcol += 1
# Autofit every sheet
with load_xl_file(xlfilepath) as wb:
for ws in wb.Sheets:
autofitendcol = ws.UsedRange.Columns.Count
ws.Range(ws.Cells(1, autofitbegcol),
ws.Cells(1, autofitendcol)).EntireColumn.AutoFit()
return xlfilepath
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