xlwt: sheet col. width

4,298 views
Skip to first unread message

Alessandro Tufi

unread,
Sep 17, 2008, 11:11:02 AM9/17/08
to python...@googlegroups.com
Hi all,
example "col_width.py" doesn't work for me (linux debian, file "xls"
opened with openoffice).

Xlwt from trunk, revision 3471


What can I do? I set the ws.col(i).width but it doesn't work, all
columns have the same width.


This is the content of the file:

from xlwt import *

w = Workbook()
ws = w.add_sheet('Hey, Dude')

for i in range(6, 80):
fnt = Font()
fnt.height = i*20
style = XFStyle()
style.font = fnt
ws.write(1, i, 'Test')
ws.col(i).width = 0x0d00 + i
w.save('col_width.xls')


Thanks
Alessandro

John Machin

unread,
Sep 17, 2008, 5:15:01 PM9/17/08
to python...@googlegroups.com
Alessandro Tufi wrote:
> Hi all,

Hi Alessandro,

> example "col_width.py" doesn't work for me (linux debian, file "xls"
> opened with openoffice).
>
> Xlwt from trunk, revision 3471
>
> What can I do? I set the ws.col(i).width but it doesn't work, all
> columns have the same width.

It does work. Unfortunately the use of a tiny increment in the example
means that the change in column width is imperceptible to the naked eye.
OOo calc says that column G is 2.55 cm wide and column CB is 2.58 cm wide.

Thanks for reporting the problem.

Here's my suggested replacement:
8<---
#!/usr/bin/env python
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('ColWidth')
for colx in range(0, 5):
width = 1600 + colx * 500
# Units: 1/256 of the width of the zero character
# in the default font (first FONT record in the file).
ws.write(0, colx, width)
ws.col(colx).width = width
wb.save('col_width.xls')
8<---

Cheers,
John

Alessandro Tufi

unread,
Sep 18, 2008, 2:58:29 AM9/18/08
to python...@googlegroups.com
From: John Machin <sjma...@lexicon.net>
Date: Wed Sep 17 2008 23:15:01 GMT+0200 (CEST)

> means that the change in column width is imperceptible to the naked eye.
> OOo calc says that column G is 2.55 cm wide and column CB is 2.58 cm wide.
>
<...>

> width = 1600 + colx * 500
> # Units: 1/256 of the width of the zero character
> # in the default font (first FONT record in the file).
>

Thanks for your answer! It does works
Before your answer I have tested the script with very little
increments.. but I saw nothing :-)

Alessandro

Reply all
Reply to author
Forward
0 new messages