Multiple formats in a cell.

228 views
Skip to first unread message

Rob

unread,
Mar 5, 2009, 11:10:24 AM3/5/09
to python-excel
Does xlwt have the ability to apply multiple formats to a cell? I am
mainly interested in being able to apply a subscript to some
characters in a string value. IE the 0 in X0.

Rob

John Machin

unread,
Mar 5, 2009, 8:13:00 PM3/5/09
to python...@googlegroups.com

Excel won't apply multiple "formats" to a cell in the sense that cell
formatting includes alignment, borders, and background. All you can vary
is the font and associated attributes, and only in a constant text cell.
Thus you could't get subscripting applied to your '0' if the 'X0' was
the result of a formula e.g. ="X"&"0".

MS Office doesn't believe in denormalisation. It has this concept of
"Rich Text", where the font etc attributes are carried around with the
text itself, to the extent that "X0"[vanilla] and "X0"[subscript from
offset 1 onwards] are two separate entries in an Excel file's SST
(shared string table).

xlwt doesn't support this.

OTTOMH, the code you would need if it did support it might look
something like this:

normal_style = xlwt.easyxf("font: name Times New Roman, height 200")
font_sub = xlwt.easyfont("escapement subscript")
font_uline_big = xlwt.easyfont("underline on, height 400")
rt = [
"Rich Text Demo: X",
("0", fnt_sub),
" and some more normal text ",
("and some 20pt underlined text", font_uline_big),
" and final normal text",
]
worksheet.write_rich_text(0, 0, rt, normal_style)

Anyone interested in the data structures should read sections
2.5.1 Formatting Runs
2.5.3 Unicode Strings (BIFF8)
4.11.1 The SST Record
5.21 CONTINUE
5.100 SST – Shared String Table
of http://sc.openoffice.org/excelfileformat.pdf

Cheers,
John

Rob

unread,
Mar 9, 2009, 4:17:20 PM3/9/09
to python-excel
I have taken your suggestions on how to implement "Rich Text" and have
been able to get it working. If you would like a copy of the changes,
please let me know and I will send it to you.

Rob

John Machin

unread,
Mar 9, 2009, 4:59:18 PM3/9/09
to python...@googlegroups.com

That's great, Rob. Yes, please, a patch against the latest source
(version 0.7.0) would be appreciated. Copies of test scripts (especially
ones that check proper CONTINUE operation on large SSTs) would be good
too.

Cheers,
John

Reply all
Reply to author
Forward
0 new messages