RichText problem with forward compatibility

1,002 views
Skip to first unread message

Chichko Liobopitko

unread,
May 12, 2022, 7:12:05 AM5/12/22
to openpyxl-users
Hello everyone!

I'm writing here, because CellRichText() works on Python 3.8.4  for me, but not 3.9.0.

When I run my basic script

workbook = Workbook()
sheet = workbook.active

label = "koprende"

color = "#2199FF"
color = InlineFont(color=f"FF{color[1:]}")  # input color format: 

label_block = TextBlock(color, label)

cell_rich_content = CellRichText("Hello, ", label_block)
sheet["A2"] = cell_rich_content
sheet["A1"] = "rich below"

workbook.save(filename="testRichText.xlsx")


On Python 3.9.0 I get the following errror

  File "C:\Users\User1\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\cell\_writer.py", line 82, in etree_write_cell
    text = r.name
AttributeError: 'TextBlock' object has no attribute 'name' 


I checked out the code and it seems, that when the script handles a TextBlock() element, it can't find the text inside. I tried dir(r) and didn't saw a name property, but there was text. I changed that. Tried again.

This time I got another error:

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

So it turns out that here node.text isn't text exactly, but xml.etree.ElementTree.Element.
Hence the code can't get the text out and the strip() method on it.

The weird thing is, that I'm getting the same errors on versions below Python 3.8.0, but I don't understand how the later versions like 3.9.0 get the same error. I suspect it's not fault of Openpyxl, but I'd really appreciate if you can help me figure it out. If it's bug in the code, I can even take some time to fix it myself, if you want. I'm open for that.

Thanks beforehand and cheers

Charlie Clark

unread,
May 12, 2022, 7:49:26 AM5/12/22
to openpyxl-users
On 12 May 2022, at 13:12, Chichko Liobopitko wrote:

> Hello everyone!
>
> I'm writing here, because *CellRichText() *works on Python *3.8.4 * for me,
>
> but not *3.9.0.*

Support for rich text will be in version 3.1, which has not been released yet and is, therefore, not supported.

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

Chichko Liobopitko

unread,
May 12, 2022, 8:29:18 AM5/12/22
to openpyxl-users
Right, I figured so. I use the 3.1 branch already (straight from the repo)
But I understand. Can you give some approximate deadline, when this function will be ready?
I really need it for my work.

Thanks for the quick reply btw

Charlie Clark

unread,
May 12, 2022, 8:37:03 AM5/12/22
to openpyxl-users
On 12 May 2022, at 14:29, Chichko Liobopitko wrote:

> Right, I figured so. I use the 3.1 branch already (straight from the repo)
>
> But I understand. Can you give some approximate deadline, when this
>
> function will be ready?
>
> I really need it for my work.

When I have the time… such is the nature of open source software.

Chichko Liobopitko

unread,
May 12, 2022, 11:55:29 AM5/12/22
to openpyxl-users
Alright, I understand you completely. 
I plan to pull the branch and try to contribute, by fixing this. 
If you don't mind and have some useful links to send, I'd be happy to go 
through them. 

Cheers and thanks for the reply

udif

unread,
May 12, 2022, 4:26:14 PM5/12/22
to openpyxl-users
You might want to read through the pull request history. You'll be able to see what files are involved in this feature.

Charlie Clark

unread,
May 13, 2022, 8:53:01 AM5/13/22
to openpyxl-users
On 12 May 2022, at 17:55, Chichko Liobopitko wrote:

> Alright, I understand you completely.
>
> I plan to pull the branch and try to contribute, by fixing this.
>
> If you don't mind and have some useful links to send, I'd be happy to go
>
> through them.

For what it's worth, tests show no difference in behaviour in supported Python versions, as you can see in the CI log. But I still have a couple of related tests in the parser to fix.

Udi Finkelstein

unread,
May 14, 2022, 6:45:47 AM5/14/22
to openpyx...@googlegroups.com
You might want to read through the pull request history. You'll be able to see what files are involved in this feature.



--
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/d9dcf894-7aa9-4a78-a510-5681413001fbn%40googlegroups.com.

Piotr Mardziel

unread,
Feb 14, 2023, 3:52:28 PM2/14/23
to openpyxl-users
I'm seeing this issue presently with version 3.1.1 on python 3.7, 3.8, and 3.9 but seems to be resolved with python 3.10 .

Piotr Mardziel

unread,
Feb 14, 2023, 3:54:58 PM2/14/23
to openpyxl-users
Sorry, it works on 3.8, 3.10 (but not on 3.7 and 3.9).

蔡皓宇

unread,
Feb 15, 2023, 2:58:47 AM2/15/23
to openpyxl-users
I have same question, and I cant run this in any Python version. Whether it's 3.8 to 3.11 all cant.

蔡皓宇

unread,
Feb 15, 2023, 2:59:57 AM2/15/23
to openpyxl-users

Miraculously, I was able to run it on the remote server, which was running Python version 3.7.11

Charlie Clark

unread,
Feb 15, 2023, 3:51:01 AM2/15/23
to '蔡皓宇' via openpyxl-users
On 15 Feb 2023, at 8:59, '蔡皓宇' via openpyxl-users wrote:

> Miraculously, I was able to run it on the remote server, which was running
>
> Python version 3.7.11

We currently have tests for Python ≥ 3.6. I have yet to see a relevant test case that is not covered by our existing unit tests and suspect that, whatever the problems are, they're not directly related to Python versions or openpyxl.

Charlie Clark

Benoit Blanchon

unread,
Feb 15, 2023, 9:16:57 AM2/15/23
to openpyxl-users
Hi,

I also have the error "AttributeError: 'TextBlock' object has no attribute 'name' " but only with etree, not with lxml.

Indeed, we can see from the stack trace that the bug is in etree_write_cell():

Now, if we look at lxml_write_cell(), we see that the code correctly calls r.text instead of r.name:

So, if you have this issue, try installing lxml.
If you don't have this issue and want to reproduce it, set the environment variable OPENPYXL_LXML to False

Best regards,
Benoit

Charlie Clark

unread,
Feb 15, 2023, 11:30:24 AM2/15/23
to openpyxl-users
On 15 Feb 2023, at 15:16, Benoit Blanchon wrote:

> Hi,
>
> I also have the error "AttributeError: 'TextBlock' object has no attribute
>
> 'name' " but only with *etree*, not with *lxml*.

Thanks for looking into this and tracking down the problem.

Charlie

蔡皓宇

unread,
Feb 15, 2023, 9:18:15 PM2/15/23
to openpyxl-users
It works. Thank you very much. Moved to cry! :)

Kushagra Singh

unread,
Mar 19, 2024, 5:17:27 PMMar 19
to openpyxl-users
Hi, I am facing a similar issue when trying to save the file with a merged cell with cell richtext formatting. What is the solution for it?

Charles Gagnon

unread,
Mar 27, 2024, 11:46:53 AMMar 27
to openpyxl-users
It works ! Thanks !
Reply all
Reply to author
Forward
0 new messages