Read about our upcoming Code of Conduct on this issue

Commit 94004b1e authored by CharlieC's avatar CharlieC
Browse files

Consolidate code for reading drawing child objects

Skip drawing containers that we can't process yet.
Resolves #1109

--HG--
branch : 2.5
parent 4208bab130bb
......@@ -8,9 +8,6 @@ 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',
......@@ -38,27 +35,3 @@ def read_chart(chartspace):
chart.backWall = cs.chart.backWall
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 = []
if rels_path in archive.namelist():
deps = get_dependents(archive, rels_path)
charts = []
for rel in drawing._chart_rels:
cs = get_rel(archive, deps, rel.id, ChartSpace)
chart = read_chart(cs)
chart.anchor = rel.anchor
charts.append(chart)
return charts
......@@ -31,14 +31,3 @@ 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
from __future__ import absolute_import
# Copyright (c) 2010-2018 openpyxl
from io import BytesIO
from warnings import warn
from openpyxl.xml.functions import fromstring
from openpyxl.xml.constants import IMAGE_NS
from openpyxl.packaging.relationship import get_rel, get_rels_path, get_dependents
from openpyxl.drawing.spreadsheet_drawing import SpreadsheetDrawing
from openpyxl.drawing.image import Image
from openpyxl.chart.chartspace import ChartSpace
from openpyxl.chart.reader import read_chart
def find_images(archive, path):
"""
Given the path to a drawing file extract anchors with images
Given the path to a drawing file extract charts and images
Ingore errors due to unsupported parts of DrawingML
"""
src = archive.read(path)
tree = fromstring(src)
drawing = SpreadsheetDrawing.from_tree(tree)
try:
drawing = SpreadsheetDrawing.from_tree(tree)
except TypeError:
warn("DrawingML support is incomplete and limited to charts and images only. Shapes and drawings will be lost.")
return [], []
rels_path = get_rels_path(path)
deps = []
if rels_path in archive.namelist():
deps = get_dependents(archive, rels_path)
charts = []
for rel in drawing._chart_rels:
cs = get_rel(archive, deps, rel.id, ChartSpace)
chart = read_chart(cs)
chart.anchor = rel.anchor
charts.append(chart)
images = []
for rel in drawing._blip_rels:
dep = deps[rel.embed]
......@@ -30,4 +47,4 @@ def find_images(archive, path):
image = Image(BytesIO(archive.read(dep.target)))
image.anchor = rel.anchor
images.append(image)
return images
return charts, images
......@@ -52,12 +52,11 @@ from openpyxl.packaging.relationship import get_dependents, get_rels_path
from openpyxl.worksheet.read_only import ReadOnlyWorksheet
from openpyxl.worksheet.table import Table
from openpyxl.drawing.spreadsheet_drawing import SpreadsheetDrawing
from openpyxl.drawing.image_reader import find_images
from openpyxl.chart.reader import find_charts
from openpyxl.xml.functions import fromstring
from .worksheet import WorkSheetParser
from .drawings import find_images
# Use exc_info for Python 2 compatibility with "except Exception[,/ as] e"
......@@ -273,9 +272,10 @@ def load_workbook(filename, read_only=False, keep_vba=KEEP_VBA,
drawings = rels.find(SpreadsheetDrawing._rel_type)
for rel in drawings:
for c in find_charts(archive, rel.target):
charts, images = find_images(archive, rel.target)
for c in charts:
ws.add_chart(c, c.anchor)
for im in find_images(archive, rel.target):
for im in images:
ws.add_image(im, im.anchor)
pivot_rel = rels.find(TableDefinition.rel_type)
......
......@@ -3,6 +3,16 @@ from __future__ import absolute_import
from zipfile import ZipFile
def test_read_charts(datadir):
datadir.chdir()
archive = ZipFile("sample.xlsx")
path = "xl/drawings/drawing1.xml"
from ..drawings import find_images
charts = find_images(archive, path)[0]
assert len(charts) == 6
def test_read_drawing(datadir):
datadir.chdir()
......@@ -10,6 +20,6 @@ def test_read_drawing(datadir):
archive = ZipFile("sample_with_images.xlsx")
path = "xl/drawings/drawing1.xml"
from ..image_reader import find_images
images = find_images(archive, path)
from ..drawings import find_images
images = find_images(archive, path)[1]
assert len(images) == 3
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