Am .06.2016, 18:06 Uhr, schrieb <
jfoxrab...@gmail.com>:
> Hi,
Hi Joe,
thanks for taking this off the issues, even if I think there probably is a
bug here.
> I have come across what is probably an uncommon use-case in openpyxl,
> and I was wondering if there is a workaround. I am trying to add a
> colored run of unicode characters to an axis title to simulate a
> simplified legend entry
> on a scatter plot.
This really is unchartered territory!
> ```
> from openpyxl import *
>
> book = workbook.Workbook()
> sheet = book.active
> chart = chart.ScatterChart()
> # Add some data to the chart.
> sheet.add_chart(chart, 'A1')
>
> chart.y_axis.title = 'blah'
>
> prop = drawing.text.CharacterProperties(solidFill='FF0000')
> run = drawing.text.RegularTextRun(rPr=prop, t='\u00b7\u00b7\u00b7\u00b7')
> chart.y_axis.title.tx.rich.paragraphs.append(drawing.text.Paragraph(r=run))
>
> book.save('test.xlsx')
> ```
>
> There are a couple of issues here. The major one is that there is no way
> to append `run` above to the existing
> `chart.y_axis.title.tx.rich.paragraphs[0]`. This could likely be fixed by
> changing
>
> ```
> r = Typed(expected_type=RegularTextRun, allow_none=True)
> ```
>
> to
>
> ```
> r = Sequence(expected_type=RegularTextRun, allow_none=True)
> ```
>
> on line 532 of `drawing.text` (`Paragraph` class), but I am not 100% sure
> about that. The minor issue is that the line `prop =
> drawing.text.CharacterProperties(solidFill='00FF00')` does not actually
> do anything.
The real problem is that very little of the drawing stuff has actually
been tested. It is a completely separate specification and is not related
to the spreadsheet stuff. openpyxl 2.3 contains just enough test coverage
to get things done and work reliably for things like titles.
The specification does indeed allow for multiple child elements but it is
the group itself that is unbounded.
<xsd:complexType name="CT_TextParagraph">
<xsd:sequence>
<xsd:element name="pPr" type="CT_TextParagraphProperties" minOccurs="0"
maxOccurs="1"/>
<xsd:group ref="EG_TextRun" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="endParaRPr" type="CT_TextCharacterProperties"
minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
I think text.CharacterProperties with a fill refers to the kind of thing
you can do with word art.
By the way all of these classes were auto-generated from the schema but
the code for this is not really very good and I was losing the will to
live when I started cleaning them up. Suffice it to say the documentation
here is very different.
While your proposed fix might work for text runs, it really needs to
handle all the possibilities to be accepted and I haven't got an idea on
how to do this with the current object model, which allows sequences but
not of mixed elements.
This stuff is so fiddly that code will not be accepted without 100%
statement coverage and I'm in no hurry to do it myself. I'd like to add
read support for charts in 2.5 but will probably do something similar to
the way we handle it with strings elsewhere and strip all formatting
information.
> By the way, before I append the new paragraph, I tried doing
>
> ```
> chart.y_axis.title.tx.rich.paragraphs[-1].br = None
> ```
>
> to simulate multiple runs by possibly eliminating the line break between
> paragraphs, but that did not appear to work at all (still got the label
> on two lines).
No idea. You really need to look at the generated XML to see what's
missing or needs adding
> I am using OpenPyXL 2.3.2 on a CentOS 6.8 machine, viewing the results on
> Windows 7, MS Office Excel 2013.
You should at least upgrade to 2.3.5, though that has no relevance here.
However, no changes will be accepted into the 2.3 branch, so you're best
working with a 2.4 checkout.
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