Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python XLWT adjusting column widths

Tags:

python

xlwt

I am enormously impressed with the ease of use of XLWT, but there is one thing I have not figured out how to do. I am trying to adjust certain rows to the minimum width they would need to display all characters (in other words, what excel would do if you double clicked on the divider between cells).

I know how to adjust the column widths to a predetermined amount, but I am not certain how to determine the minimum width needed to display everything.

like image 515
TimothyAWiseman Avatar asked Jun 30 '10 23:06

TimothyAWiseman


People also ask

How do I change the width of a column in 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 I use XLWT module in Python?

Using xlwt module, one can perform multiple operations on spreadsheet. For example, writing or modifying the data can be done in Python. Also, the user might have to go through various sheets and retrieve data based on some criteria or modify some rows and columns and do a lot of work.

What is import XLWT?

This is a library for developers to use to generate spreadsheet files compatible with Microsoft Excel versions 95 to 2003. The package itself is pure Python with no dependencies on modules or packages outside the standard Python distribution.


1 Answers

Width is 1/256 the width of the zero character for the default font. A good enough approximation is:

def get_width(num_characters):
    return int((1+num_characters) * 256)
like image 95
Manfre Avatar answered Oct 05 '22 07:10

Manfre