Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

Commit 60c34e5f authored by CharlieC's avatar CharlieC
Browse files

Add preliminary support for reading charts.

--HG--
branch : 2.4
parent a018d4069f2b
......@@ -22,8 +22,9 @@ from openpyxl.descriptors import (
)
from openpyxl.descriptors.excel import (
Percentage,
ExtensionList
)
ExtensionList,
Relation
)
from openpyxl.descriptors.nested import (
NestedBool,
......@@ -364,9 +365,26 @@ class ExternalData(Serialisable):
self.id = id
class RelId(Serialisable):
class PageMargins(Serialisable):
"""
Identical to openpyxl.worksheet.page.Pagemargins but element names are different :-/
"""
tagname = "pageMargins"
l = Float()
r = Float()
t = Float()
b = Float()
header = Float()
footer = Float()
pass # todo
def __init__(self, l=0.75, r=0.75, t=1, b=1, header=0.5, footer=0.5):
self.l = l
self.r = r
self.t = t
self.b = b
self.header = header
self.footer = footer
class PrintSettings(Serialisable):
......@@ -407,7 +425,7 @@ class ChartSpace(Serialisable):
textProperties = Alias("txPr")
externalData = Typed(expected_type=ExternalData, allow_none=True)
printSettings = Typed(expected_type=PrintSettings, allow_none=True)
userShapes = Typed(expected_type=RelId, allow_none=True)
userShapes = Relation()
extLst = Typed(expected_type=ExtensionList, allow_none=True)
__elements__ = ('date1904', 'lang', 'roundedCorners', 'style',
......
from __future__ import absolute_import
# Copyright (c) 2010-2016 openpyxl
"""
Read a chart
"""
from .chartspace import ChartSpace, PlotArea
from openpyxl.xml.functions import fromstring
_types = ('areaChart', 'area3DChart', 'lineChart', 'line3DChart',
'stockChart', 'radarChart', 'scatterChart', 'pieChart', 'pie3DChart',
'doughnutChart', 'barChart', 'bar3DChart', 'ofPieChart', 'surfaceChart',
'surface3DChart', 'bubbleChart',)
_axes = ('valAx', 'catAx', 'dateAx', 'serAx',)
def reader(src):
node = fromstring(src)
cs = ChartSpace.from_tree(node)
plot = cs.chart.plotArea
for t in _types:
chart = getattr(plot, t, None)
if chart is not None:
break # this ignores multiple charts
chart.title = cs.chart.title
chart.layout = plot.layout
chart.legend = cs.chart.legend
for x in _axes:
ax = getattr(plot, x)
if ax:
if x == 'valAx':
chart.y_axis = ax[0]
elif x == 'serAx':
chart.z_axis = ax[0]
else:
chart.x_axis = ax[0]
return chart
This diff is collapsed.
from __future__ import absolute_import
# Copyright (c) 2010-2016 openpyxl
from .. line_chart import LineChart
from .. axis import NumericAxis, DateAxis
def test_read(datadir):
datadir.chdir()
from .. reader import reader
with open("chart1.xml") as src:
xml = src.read()
chart = reader(xml)
assert isinstance(chart, LineChart)
assert chart.title.tx.rich.p[0].r.t == "Website Performance"
assert isinstance(chart.y_axis, NumericAxis)
assert chart.y_axis.title.tx.rich.p[0].r.t == "Time in seconds"
assert isinstance(chart.x_axis, DateAxis)
assert chart.x_axis.title is None
assert len(chart.series) == 10
......@@ -8,6 +8,7 @@ from openpyxl.descriptors import (
Integer,
String,
NoneSet,
Alias,
)
from openpyxl.descriptors.excel import UniversalMeasure, Relation
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment