Sheet.write_boolean(row, col, value, format)Įlif isinstance(value, datetime) or isinstance(value, timedelta): Sheet.write_number(row, col, value, format) Sheet.write_string(row, col, value, format)Įlif isinstance(value, int) or isinstance(value, float): Sheet.write_blank(row, col, value, format) lWidths = max(lWidths, get_cell_width(value, format)) Worksheet.add_write_handler(stdtype, colWidthTracker)ĭef colWidthTracker(sheet, row, col, value, format): lWidths = *number_of_used_columnsįor stdtype in : # add worksheet attribute to store column widths This way, you only need to read and set the optimal column width in the end (before closing the workbook). In the handler function, you simply pass on the write command, but also do the bookkeeping wrt. You can register a write handler for all standard types. (A) Register a write handler to do the job: If you do not like to keep track within your own data structure, there are at least three ways to go: It would be better if I could just loop through all the cells, that way a generic routine could be written. This means I need to keep track of each cell width as I write the cell. Xlsxwriter does not appear to have a method to read back a particular cell. And there might be merged cells spanning multiple columns. Also, it might be faster to use a lookup table format->font for your workbook, so that you do not have to define the used font every single time.įinally, one could take care of line breaks within the cell string: pixelwidths = (used_asure(part) for part in cell_string.split('\n'))Ĭell_width = (max(pixelwidths) used_asure(' '))/reference_asure('0')Īlso, if you are using the Excel filter function, the dropdown arrow symbol needs another 18 pixels (at 100% zoom in Excel). Of course you would like to get the root handling and reference font creation out of the function, if it is meant to be executed frequently. Used_font = (family = format.font_name,Ĭell_width = used_asure(cell_string ' ')/reference_asure('0') Reference_font = (family='Calibri', size=11) This way the results are actually very close to Excel's autofit results, so that I assume Excel is doing just that.įor the tkinter magic to work, a tkinter.Tk() instance (a window) has to be open, therefore the full code for a function that returns the required width of a cell would look like this: import tkinterĭef get_cell_width(cell_string, format = None): The whitespace is added to the string to provide some margin. Slant = ('italic' if alic else 'roman'),Īnd afterwards determine the cell width as cell_width = used_asure(cell_string ' ')/reference_asure('0') Weight = ('bold' if format.bold else 'normal'), That means, if you wrote something to your table using worksheet.write(row, col, cell_string, format), you can get the used font like this: used_font = (family = format.font_name, In order to do this for a cell from your Excel table, you need to take its format into account (it contains all the information on the used font). You can afterwards use its measure method to determine string widths in pixels, e.g. reference_font = (family='Calibri', size=11) In order to get a handle on the exact width of a string, you can use tkinter's ability to measure string lengths in pixels, depending on the font/size/weight/etc. I can not find a way to measure the width of the item that I want to insert into the cell. The column widths are given in multiples of the width of the '0' character in the font Calibri, size 11 (that's the Excel standard). That URL does not specify what the units are for the third argument to set_column. values] ) for col in lumns]įor i, width in enumerate(get_col_widths(dataframe)): # Then, we concatenate this to the max of the lengths of column name and its values for each column, left to right # First we find the maximum length of the index column It shouldn't be too difficult to adapt this code for whatever data you are using. Then, it returns the maximum of all values and the column name for each of the remaining columns moving left to right. It first finds the maximum width of the index, which is always the left column for a pandas to excel rendered dataframe. Per example, I tend to use the code below when working with pandas dataframes and xlsxwriter. So, you can simulate autofit by setting each column to the max number of characters in that column. The with of 1 unit of the xlsxwriter columns is about equal to the width of one character. As a general rule, you want the width of the columns a bit larger than the size of the longest string in the column.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |