Zoom in xlwt + auto-adjusting width for columns.

5,555 views
Skip to first unread message

Dargoan

unread,
Nov 13, 2009, 7:27:16 AM11/13/09
to python-excel
Hey everyone,

I'm new here. I'm new in Python, and I'm new in xlwt.
I'm using Python 2.5 on a windows OS (XP) for work. I'm using xlwt
0.7.2.

I looked in the source code but found nothing related to my questions,
and also in the examples. So I'm asking here.

Q1:
I want to create a Excel file. I manage to do it, but I'd like to
create a file with 85% of the Zoom property for the Spreadsheet.
Does xlwt permit that and how?

Q2:
I'd like some of my columns to adjust their width automatically. Same
question: does xlwt permit that and how?

Many thanks !

John Machin

unread,
Nov 13, 2009, 9:32:48 AM11/13/09
to python...@googlegroups.com
On 13/11/2009 11:27 PM, Dargoan wrote:
> Hey everyone,
>
> I'm new here. I'm new in Python, and I'm new in xlwt.

Hey Dargoan,

Welcome ...

> I'm using Python 2.5 on a windows OS (XP) for work. I'm using xlwt
> 0.7.2.
>
> I looked in the source code but found nothing related to my questions,
> and also in the examples. So I'm asking here.
>
> Q1:
> I want to create a Excel file. I manage to do it, but I'd like to
> create a file with 85% of the Zoom property for the Spreadsheet.
> Does xlwt permit that and how?

(1) ...../xlwt/examples/zoom_magnification.py

(2) Search for messages in this group with "zoom" in the subject, dates
in December 2008.

>
> Q2:
> I'd like some of my columns to adjust their width automatically. Same
> question: does xlwt permit that and how?

No. The autofit is not an option which can be set in the file so that an
autofit is done when the file is opened. Doing that requires a UI
rendering engine, which is unlikely ever to be found in xlwt.

Cheers,

John

>
> Many thanks !
>
> >
>
>

Dargoan

unread,
Nov 13, 2009, 10:17:05 AM11/13/09
to python-excel
Many thanks John.

I looked for keyword "zoom" in this google group, and actually, I
managed to put some zoom in my file, for all worksheets!

I used :
wb = Workbook(encoding="utf-8")
ws = wb.add_sheet(mySheetName)
ws.normal_magn=85

to obtain a 85% zoom in my worksheet.

Note that, for some reason, I don't have the file
zoom_magnification.py in the examples folder.
Best regards

John Machin

unread,
Nov 13, 2009, 10:47:26 AM11/13/09
to python...@googlegroups.com
On 14/11/2009 2:17 AM, Dargoan wrote:

>
> Note that, for some reason, I don't have the file
> zoom_magnification.py in the examples folder.

How did you install xlwt, and where did you install xlwt?


John Yeung

unread,
Nov 13, 2009, 7:46:56 PM11/13/09
to python...@googlegroups.com
On Fri, Nov 13, 2009 at 7:27 AM, Dargoan <johan.c...@gmail.com> wrote:
>
> Q2:
> I'd like some of my columns to adjust their width automatically. Same
> question: does xlwt permit that and how?

I think John Machin probably hates that I've done this, and hates more
that I mention it when people ask, but I've gathered approximate
widths for common characters in Arial 10 (most people's default Excel
font) and provided it in the arial10.py file in the files section of
this group, so you can build your own autofit routines. (I included a
very rudimentary one in arial10.py.) The process is quite ugly and
slow: You have to calculate the width of each cell as you're writing
it, keeping track of the maximum width and using that to set the
column width after you have finished writing all the cells in that
column.

Depending on the data being fitted and the formatting you're using, it
can be much trickier and more error-prone than it sounds. If your
needs are complex (using formulas, for example) then it becomes
impossible or close to it. At some point, it's simpler, easier, and
faster to just use Excel itself to do this task (it can be controlled
programmatically by various means, including Python using COM).

John Y.

John Machin

unread,
Nov 13, 2009, 8:54:49 PM11/13/09
to python...@googlegroups.com
On 14/11/2009 11:46 AM, John Yeung wrote:
> On Fri, Nov 13, 2009 at 7:27 AM, Dargoan <johan.c...@gmail.com> wrote:
>> Q2:
>> I'd like some of my columns to adjust their width automatically. Same
>> question: does xlwt permit that and how?
>
> I think John Machin probably hates that I've done this,

You think incorrectly. It's a brave, noble, heroic effort. But
misdirected IMHO. "C'est magnifique, mais ce n'est pas la guerre."

> and hates more
> that I mention it when people ask,

Not at all. I do regret that people don't search for previous discussion.

> but I've gathered approximate
> widths for common characters in Arial 10 (most people's default Excel
> font) and provided it in the arial10.py file in the files section of
> this group, so you can build your own autofit routines. (I included a
> very rudimentary one in arial10.py.) The process is quite ugly and
> slow: You have to calculate the width of each cell as you're writing
> it, keeping track of the maximum width and using that to set the
> column width after you have finished writing all the cells in that
> column.
>
> Depending on the data being fitted and the formatting you're using, it
> can be much trickier and more error-prone than it sounds. If your
> needs are complex (using formulas, for example) then it becomes
> impossible or close to it. At some point, it's simpler, easier, and
> faster to just use Excel itself to do this task (it can be controlled
> programmatically by various means, including Python using COM).

Or Python using pyuno to drive OpenOffice ...

Cheers,
John

Reply all
Reply to author
Forward
0 new messages