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 d42b1bd8 authored by CharlieC's avatar CharlieC
Browse files

Initial implementation for finding charts.

--HG--
branch : 2.5
parent 104b36d52e2b
......@@ -8,6 +8,9 @@ Read a chart
from .chartspace import ChartSpace, PlotArea
from openpyxl.xml.functions import fromstring
from openpyxl.packaging.relationship import get_rel, get_rels_path, get_dependents
from openpyxl.drawing.spreadsheet_drawing import SpreadsheetDrawing
_types = ('areaChart', 'area3DChart', 'lineChart', 'line3DChart',
'stockChart', 'radarChart', 'scatterChart', 'pieChart', 'pie3DChart',
'doughnutChart', 'barChart', 'bar3DChart', 'ofPieChart', 'surfaceChart',
......@@ -16,7 +19,7 @@ _types = ('areaChart', 'area3DChart', 'lineChart', 'line3DChart',
_axes = ('valAx', 'catAx', 'dateAx', 'serAx',)
def reader(src):
def read_chart(src):
node = fromstring(src)
cs = ChartSpace.from_tree(node)
plot = cs.chart.plotArea
......@@ -39,3 +42,31 @@ def reader(src):
else:
chart.x_axis = ax[0]
return chart
def find_charts(archive, path):
"""
Given the path to a drawing file extract anchors with charts
"""
src = archive.read(path)
tree = fromstring(src)
drawing = SpreadsheetDrawing.from_tree(tree)
rels_path = get_rels_path(path)
deps = get_dependents(archive, rels_path)
charts = []
for anchor in drawing.twoCellAnchor:
if anchor.graphicFrame is not None:
graphic = anchor.graphicFrame.graphic
if graphic.graphicData is not None:
id = graphic.graphicData.chart.id
chart = get_rel(archive, deps, id, ChartSpace)
charts.append(chart)
anchor.graphicFrame = None
chart.anchor = anchor
return charts
......@@ -136,7 +136,8 @@ class Series(Serialisable):
bubble3D=None,
marker=None,
smooth=None,
explosion=None
explosion=None,
extLst=None,
):
self.idx = idx
self.order = order
......
from __future__ import absolute_import
# Copyright (c) 2010-2017 openpyxl
from zipfile import ZipFile
from .. line_chart import LineChart
from .. axis import NumericAxis, DateAxis
def test_read(datadir):
datadir.chdir()
from .. reader import reader
from .. reader import read_chart
with open("chart1.xml") as src:
xml = src.read()
chart = reader(xml)
chart = read_chart(xml)
assert isinstance(chart, LineChart)
assert chart.title.tx.rich.p[0].r.t == "Website Performance"
......@@ -23,3 +25,14 @@ def test_read(datadir):
assert chart.x_axis.title is None
assert len(chart.series) == 10
def test_read_drawing(datadir):
datadir.chdir()
archive = ZipFile("sample.xlsx")
path = "xl/drawings/drawing1.xml"
from ..reader import find_charts
charts = find_charts(archive, path)
assert len(charts) == 6
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