Sorry to come back with my question.
I looked over the Support Rich Text in Cells PR and tried to adapt it.
The issue that I am facing is that the PR is based on usage of `openpyxl.writer.strings::
write_string_table `.
It basically stops the normal flow in case the `key` or `cell.value` is a `RichTextContent` and directly does `xf.write(key.to_tree())`
I figured that to achieve the same thing I need to change the current `openpyxl.cell._writer:write_cell` method and did something like this (disclaimer: it's just-see-if-it-works-first code)
from openpyxl.cell._writer import write_cell
from openpyxl.worksheet import _writer
from .rich_text import RichTextContent
def with_rich_text_writer(xf, worksheet, cell, styled=None):
if isinstance(cell.value, RichTextContent):
xf.write(cell.value.to_tree())
else:
return write_cell(xf, worksheet, cell, styled)
_writer.write_cell = with_rich_text_writer
# Then a simple test
wb = Workbook()
sheet = wb.active
cell = sheet.cell(1,1)
bold_red = Font(name='Calibri',size=11,bold=True,italic=False,vertAlign=None,underline='none',strike=False,color='FFFF0000')
# Setting directly _value for now
cell._value = RichTextContent(["Normal", ("Bold and Red", bold_red)])
# Like what is done in the PR in _bind_value
cell.data_type = "s"
save_workbook(wb, "temp.xlsx")
The problem is that nothing get's written at all.
This is not that surprising given the fact that the logic of `openpyxl.cell.write_cell` seems different from the older `
write_string_table `. Now it seems to always be a `Element("c", attrs)` when `RichTextContent.to_tree` does this
def to_tree(self):
"""Used by write_shared_string to produce resulting XML.
"""
if len(self) == 1 and self._elements[0].is_plain:
# Pack as plain text
return Text(t=self._elements[0].text).to_tree("si")
text_element = Text()
for element in self._elements:
if not element.is_plain:
inline_font = self._font_to_inline_font(element.font)
else:
inline_font = None
rich_text = RichText(rPr=inline_font, t=element.text)
text_element.r.append(rich_text)
return text_element.to_tree("si")
I tried changing to `to_tree("c")` , even trying to see if it's because of the attributes returned by `_set_attributes` and added the coordinate directly on the element returned by `text_element.to_tree("c")` but always same result.
I am asking in case there is something simple I don't see because of my lack of knowledge and that would make the whole thing work ?
Again it's just a does-it-work-at-all code I need for now.