Resizing height of rows

3,197 views
Skip to first unread message

Max Leason

unread,
Jun 23, 2008, 12:21:04 PM6/23/08
to python-excel
I have been having trouble changing the height of a given row. In an
attempt to automatically resize a row so that all the text of the cell
is visible.

This is a simplified version of the code that doesn't work:

str = "This is a very very very very very long string to test the auto
resizing feature"
rows = math.ceil(len(str) / 20) #20 is the number of characters to
fit in the cell's width
twips_per_row = 255 #default row height for 10 point font
new_row_height = int(rows * twips_per_row)
ws.row(0).height = new_row_height
ws.write(0,0, str)

So this should resize the row so that the entire text is visible with
test wrap turned on, but it really doesn't do anything apparent. Any
help on this would be great or even a better way to do the same thing
would be great too.

Thanks,
Max Leason

John Machin

unread,
Jun 23, 2008, 8:24:07 PM6/23/08
to python...@googlegroups.com

One possibility is that you didn't actually turn on wrap. Supplying
minimal executable code that demonstrates the alleged problem is always
a very good idea -- it saves you being asked things like "how did you
attempt to turn on wrap" and saves answerers from having to type in
extra glue code.

It appears that to get any visual effect of a change to row height, you
need to set the height_mismatch flag on the row. However, fiddling with
the row height is pointless. You need to set the column widths to
whatever you want. Excel adjusts the row height to accomodate the
tallest cell. For example, open a new sheet, and type "The quick brown
fox", "waffle waffle" and "Now is the winter of our discontent made
glorious summer by this son of York" into cells A1, B1, and C1
respectively. Then select A1 and C1 and go Format/Cells/Alignment, tick
the "Wrap text" box, and click OK. You should see that row 1 is resized
from 1 line to the 8 lines required for C1.

I've attached some sample code that might give you some ideas. Note that
working out what column width you need to use requires guesswork and
calibration. Do let us know how you get on.

Cheers,
John

wraptest.py

Max Leason

unread,
Jun 23, 2008, 6:47:05 PM6/23/08
to python-excel
A couple small corrections:
The problem only occurs when write_merge is used

al = Alignment()
al.horz = Alignment.HORZ_LEFT
al.vert = Alignment.VERT_TOP
al.wrap = 1

style = XFStyle()
style.alignment = al

ws.write(0,0, str) should be replaced with,
ws.write_merge(0, 0, 0, 4, str, style)

Thanks,
Max Leason

John Machin

unread,
Jun 24, 2008, 6:21:53 PM6/24/08
to python...@googlegroups.com

Have you tried to do this auto-resizing of a merged cell region using
the Excel / OOo Calc / Gnumeric UI? If so, please list the actions
required. If not, ...

AND (again) please supply the minimum executable code that is attempting
to achieve your goal but isn't.

Max Leason

unread,
Jun 27, 2008, 10:09:05 AM6/27/08
to python-excel
In Excel 2003 there is no way to auto-resize merged cells in the
traditional way, my next idea is to just merge the cell vertically as
well as horizontally. This seems like it might be the better option
since i already came up with a way to calculate the number of rows
needed to display the full text of the cell.

As for the displaying the minimum executable code, I'm not sure how to
attach a file like you did earlier in this post so for now im just
going to have to copy and paste the source.

#!/usr/bin/env python

import math
from xlwt import *

fnt = Font()
fnt.name = 'Arial'
fnt.colour_index = 0
fnt.bold = True

borders = Borders()
borders.left = 0
borders.right = 0
borders.top = 0
borders.bottom = 0

al = Alignment()
al.horz = Alignment.HORZ_CENTER
al.vert = Alignment.VERT_CENTER
al.wrap = 1

style = XFStyle()
style.alignment = al
style.font = fnt
style.borders = borders

wb = Workbook()
ws = wb.add_sheet('sheet0')


str = "This is a very very very very very long string to test the auto
resizing feature"
rows = math.ceil(len(str) / 40) - 1

#This was my initial idea to display all the text of the cell
twips_per_row = 255 #default row height for 10 point font
new_row_height = int(rows * twips_per_row)
ws.row(0).height = new_row_height
ws.write_merge(0, 0, 0, 3, str, style)

#This is my new idea
ws.write_merge(1, 1+rows, 0, 3, str, style)

wb.save('row_height.xls')

I think the newer idea has promise but neither solution is prefect.
Reply all
Reply to author
Forward
0 new messages