Read partly bold cell

1,266 views
Skip to first unread message

Gleb Stepanov

unread,
Jun 26, 2015, 8:59:57 AM6/26/15
to python...@googlegroups.com
Hi everyone!
How to get information from xlsx cell about which words (there positions) are bolded?
I've used xlrd for reading xlsx.


Charlie Clark

unread,
Jun 26, 2015, 9:02:50 AM6/26/15
to python...@googlegroups.com
I think xlrd doesn't support formatting for xlsx files you may have to use
openpyxl. Unfortunately, there is no other way of doing things than
looping over all the cells. Something like this should work.

wb = load_workbook(…)
ws = wb.active

for row in ws:
for cell in row:
if cell.font.bold:
# whatever logic you need

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

John Yeung

unread,
Jun 26, 2015, 10:02:31 AM6/26/15
to python-excel
On Fri, Jun 26, 2015 at 9:02 AM, Charlie Clark
<charli...@clark-consulting.eu> wrote:
> Am .06.2015, 14:49 Uhr, schrieb Gleb Stepanov <gleb.s...@gmail.com>:
>> How to get information from xlsx cell about which words (there positions)
>> are bolded?
>> I've used xlrd for reading xlsx.
>
> I think xlrd doesn't support formatting for xlsx files

This is correct, at least for now. I don't know if there are plans to
change this. I certainly do not expect it to change soon.

> you may have to use
> openpyxl. Unfortunately, there is no other way of doing things than looping
> over all the cells.

You've misunderstood the question. The title actually states it quite
clearly: "Read partly bold cell". That is, within ONE cell, he would
like to be able to tell which characters are bold and which are not.

It's a Rich Text formatting question. I don't know OpenPyXL well
enough to answer it though.

John Y.

Charlie Clark

unread,
Jun 26, 2015, 10:05:13 AM6/26/15
to python...@googlegroups.com
Am .06.2015, 16:02 Uhr, schrieb John Yeung <gallium....@gmail.com>:

> You've misunderstood the question. The title actually states it quite
> clearly: "Read partly bold cell". That is, within ONE cell, he would
> like to be able to tell which characters are bold and which are not.

> It's a Rich Text formatting question. I don't know OpenPyXL well
> enough to answer it though.

We don't support it. I can't think of a sane OO interface for it. It would
be easy enough to hack into a particular project.

Gleb Stepanov

unread,
Jun 26, 2015, 10:20:04 AM6/26/15
to python...@googlegroups.com
I am sad to hear that there is no way to read formating of partly bold cell.

Actually I could use any table format which is supported by MS Excel 2010. Which module for python and table file format should I choose in case of supporting internal cell formatting?

Charlie Clark

unread,
Jun 26, 2015, 10:25:26 AM6/26/15
to python...@googlegroups.com
Am .06.2015, 16:20 Uhr, schrieb Gleb Stepanov <gleb.s...@gmail.com>:

> I am sad to hear that there is no way to read formating of partly bold
> cell.

You could add it locally without much effort. This is the advantage of
open source.

> Actually I could use any table format which is supported by MS Excel
> 2010.
> Which module for python and table file format should I choose in case of
> supporting internal cell formatting?

Table style are currently not supported in openpyxl which means they'll be
ignored.

Can you give an example? None of the builtins table styles have internal
formatting (because it's not possible).

Gleb Stepanov

unread,
Jun 26, 2015, 10:39:46 AM6/26/15
to python...@googlegroups.com
As example, i've got cell like this: Something bold in the cell.
I want to read the cell and send the string further through json to web-form with the format like this: {"test-string":"Something <b>bold</b> in the cell."}


пятница, 26 июня 2015 г., 17:25:26 UTC+3 пользователь Charlie Clark написал:

Charlie Clark

unread,
Jun 26, 2015, 10:50:58 AM6/26/15
to python...@googlegroups.com
Am .06.2015, 16:39 Uhr, schrieb Gleb Stepanov <gleb.s...@gmail.com>:

> As example, i've got cell like this: Something *bold* in the cell.
> I want to read the cell and send the string further through json to
> web-form with the format like this: {"test-string":"Something
> <b>bold</b> in
> the cell."}

This has nothing to do with table styles. As I said earlier, you could
easily add such a feature to a checkout of openpyxl yourself. Or contract
someone else to do it.

John Yeung

unread,
Jun 26, 2015, 11:22:03 AM6/26/15
to python-excel
On Fri, Jun 26, 2015 at 10:39 AM, Gleb Stepanov <gleb.s...@gmail.com> wrote:
> As example, i've got cell like this: Something bold in the cell.
> I want to read the cell and send the string further through json to web-form
> with the format like this: {"test-string":"Something <b>bold</b> in the
> cell."}

Your requirements are almost exactly the same as what is in this Stack
Overflow question:

http://stackoverflow.com/questions/5629084/reading-rich-text-from-an-excel-cell

Maybe the responses there will give you some ideas, if you're willing
to stray from Python or investigate whether Microsoft-centric
solutions can be adapted for tools like pywin32 or xlwings. In any
case, if you want to do further research, you should be adding the
term "rich text" to your search.

I think an easier path, if you have access to the actual Excel program
(not just the workbook file), is to save the file in another format,
such as "XML Spreadsheet". This is much easier to parse (though not
with xlrd or OpenPyXL). It is even already close to what you want. For
example, this is what a cell looks like in that format:

<Cell><ss:Data ss:Type="String"
xmlns="http://www.w3.org/TR/REC-html40"><Font
html:Color="#000000">Something </Font><B><Font
html:Color="#000000">bold</Font></B><Font
html:Color="#000000"> in the cell.</Font></ss:Data></Cell>

John Y.
Reply all
Reply to author
Forward
0 new messages