viking-sudo-rm / rtflib

Python library for writing RTF files.
MIT License
8 stars 4 forks source link

color in table almost works #7

Closed Neon22 closed 1 month ago

Neon22 commented 1 month ago

BTW color in a cell does not come across and appear in the output. E.g.

rtf = Rtf()
rtf.add(Line("here is a table:"))
rtf.add(Table(
Row(Line("hello"), Line("world")),
Row(Line("hallo", color=Color(255, 0, 0)), Line("Welt", color=Color(0, 255, 0))),
))

Hmmm... Looks like it should work. Asks the Line for its rtf_code but it hasn't been rtf'd yet and so returns a plain string ??

viking-sudo-rm commented 1 month ago

Hmm, weird. Do the markup characters get generated in the RTF output? It looks like they should.

If they're there but the color isn't rendering properly, something might be off about the syntax and it would be worth looking at a minimal example of an RTF with colored text inside a table

Neon22 commented 1 month ago

Alas no - the call to cell.rtf_code returns a single text string of the text for the cell. These two generate exactly the same output:

rtf = Rtf()
rtf.add(Line("here is a table:"))
rtf.add(Table(
    Row(Line("hello"), Line("world")),
    Row(Line("hallo"), Line("Welt")),
))
rtf = Rtf()
rtf.add(Line("here is a table:"))
rtf.add(Table(
    Row(Line("hello", color=Color(255, 0, 0)), Line("world", color=Color(0, 255, 0))),
    Row(Line("hallo"), Line("Welt")),
))

The output is:

{\rtf1\ansi\deff0
here is a table:
\
\trowd
\cellx1000\cellx2000
\pard\intbl{hello}\cell
\pard\intbl{world}\cell
\row
\trowd
\cellx1000\cellx2000
\pard\intbl{hallo}\cell
\pard\intbl{Welt}\cell
\row

}

Note that the colors are not gathered either. It appears that the Line() is not being processed as an RtfElement if its inside a Table().

viking-sudo-rm commented 1 month ago

The issue is that Line renders color based on the __cid__ field rather than the color field, which doesn't get set within a table

Neon22 commented 1 month ago

Note that my aim is here is purely to get BGcolors on alternating table lines for clarity - and not to lose my mind implementing all the things :) Now that I have borders and padding in table - almost there.

Hmmm. reading up - it looks like the background color (say of a table cell) is given by a tag formatted like \clcbpat8 where the number is the index into the color table.

maybe the same color gathering you do as a preprocess could also gather any colors mentioned in the table. Is this possible ? Or is it better to add a color in a sep op and get the cid back then use that to mark a cell similar to what I am trying with the borders etc. ??

I got my color table info from:

I am using Libreoffice to do my testing and doing compares between rtf files with diff flags.

Yeah I'm not sure its going to be easy to color text in tables. In Libreoffice it looks like they do it with style sheets so that's a bridge too far I think. Maybe a workaround for cell background color ? What's your opinion ?

viking-sudo-rm commented 1 month ago

I think a workaround for cell background color would be a good compromise for now. That's probably more common anyway compared to adding colored text within a table

Neon22 commented 1 month ago

add a PR to enable color of row of cells. That's probably it. Thanks heaps

viking-sudo-rm commented 1 month ago

Closed based on #11