RichText - Cell Formatting

86 views
Skip to first unread message

Roberto Sandoval

unread,
Mar 22, 2023, 12:08:29 PM3/22/23
to openpyxl-users
Good morning,

I have tried formatting a cell with openpyxl RichText, using CellRichText and TextBlock, but I always get the same error message:

 File "C:\Users\robesand\AppData\Local\Programs\Python\Python38\lib\site-packages\openpyxl\xml\functions.py", line 85, in whitespace
    stripped = node.text.strip()
AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'strip'

Code Snippet 1:
from openpyxl import Workbook
from openpyxl.cell.text import InlineFont
from openpyxl.cell.rich_text import CellRichText, TextBlock

# Create a new workbook
wb = Workbook()

# Select the active worksheet
ws = wb.active

rich_string1 = CellRichText(
    'This is a test ',
    TextBlock(InlineFont(b=True), 'xxx'),
   'yyy'
)

ws['A1'] = rich_string1

wb.close()

# Save the workbook
wb.save('example.xlsx')

Code Snippet 2:
        #Define the RichText
        rt = CellRichText()
        tb1 = TextBlock("Summary: \n", InlineFont(b=True, rFont='Calibri', sz=10))
        rt.append(tb1)
        tb2 = TextBlock(summary_text, InlineFont(b=False, rFont='Calibri', sz=10))
        rt.append(tb2)
        vuln_sheet.cell(row=11, column=2, value=rt)

Could you provide any workaround?

Best,

Udi Finkelstein

unread,
Mar 22, 2023, 12:34:41 PM3/22/23
to openpyx...@googlegroups.com
Yes, install lxml.
pip install lxml

As the one who contributed most of the code for RichText, I'm the one to blame.
openpyxl is designed to work with either lxml or etree, but once you install lxml, it takes precedence.
Since I had lxml installed when developing this, there were several cases where etree support had a few bugs.

Udi


--
You received this message because you are subscribed to the Google Groups "openpyxl-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openpyxl-user...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openpyxl-users/81f90fef-cde7-42dc-90bd-b49759f92ab0n%40googlegroups.com.

Charlie Clark

unread,
Apr 13, 2023, 2:04:26 PM4/13/23
to openpyx...@googlegroups.com
On 22 Mar 2023, at 17:34, Udi Finkelstein wrote:

> Yes, install lxml.
> pip install lxml
>
> As the one who contributed most of the code for RichText, I'm the one to
> blame.
> openpyxl is designed to work with either lxml or etree, but once you
> install lxml, it takes precedence.
> Since I had lxml installed when developing this, there were several cases
> where etree support had a few bugs.

I've finally got round to adding relevant tests and fixing this for those without lxml, I hope!

It demonstrates, yet again, how important tests are for this, but also the importance, in an OO application, of being able to implement methods whenever possible on objects themselves. This makes testing and coding a whole lot easier and would have avoided the problems in the first place.

CellRichText is still test poor, but it's getting better.

Charlie

--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Sengelsweg 34
Düsseldorf
D- 40489
Tel: +49-203-3925-0390
Mobile: +49-178-782-6226

Charlie Clark

unread,
Apr 19, 2023, 12:02:13 PM4/19/23
to Udi Finkelstein, openpyx...@googlegroups.com
On 22 Mar 2023, at 17:34, Udi Finkelstein wrote:

> Yes, install lxml.
> pip install lxml
>
> As the one who contributed most of the code for RichText, I'm the one to
> blame.
> openpyxl is designed to work with either lxml or etree, but once you
> install lxml, it takes precedence.
> Since I had lxml installed when developing this, there were several cases
> where etree support had a few bugs.

Udi,

when you have some time, could you take a look at writing tests for the _opt method? I think this is a pretty neat feature of the CellRichText but it's difficult to describe and I'd hate to to have fix anything without tests.
Reply all
Reply to author
Forward
0 new messages