Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/openpyxl/reader/drawings.py: 20%
50 statements
« prev ^ index » next coverage.py v7.3.3, created at 2023-12-20 06:34 +0000
« prev ^ index » next coverage.py v7.3.3, created at 2023-12-20 06:34 +0000
2# Copyright (c) 2010-2023 openpyxl
5from io import BytesIO
6from warnings import warn
8from openpyxl.xml.functions import fromstring
9from openpyxl.xml.constants import IMAGE_NS
10from openpyxl.packaging.relationship import get_rel, get_rels_path, get_dependents
11from openpyxl.drawing.spreadsheet_drawing import SpreadsheetDrawing
12from openpyxl.drawing.image import Image, PILImage
13from openpyxl.chart.chartspace import ChartSpace
14from openpyxl.chart.reader import read_chart
17def find_images(archive, path):
18 """
19 Given the path to a drawing file extract charts and images
21 Ignore errors due to unsupported parts of DrawingML
22 """
24 src = archive.read(path)
25 tree = fromstring(src)
26 try:
27 drawing = SpreadsheetDrawing.from_tree(tree)
28 except TypeError:
29 warn("DrawingML support is incomplete and limited to charts and images only. Shapes and drawings will be lost.")
30 return [], []
32 rels_path = get_rels_path(path)
33 deps = []
34 if rels_path in archive.namelist():
35 deps = get_dependents(archive, rels_path)
37 charts = []
38 for rel in drawing._chart_rels:
39 try:
40 cs = get_rel(archive, deps, rel.id, ChartSpace)
41 except TypeError as e:
42 warn(f"Unable to read chart {rel.id} from {path} {e}")
43 continue
44 chart = read_chart(cs)
45 chart.anchor = rel.anchor
46 charts.append(chart)
48 images = []
49 if not PILImage: # Pillow not installed, drop images
50 return charts, images
52 for rel in drawing._blip_rels:
53 dep = deps[rel.embed]
54 if dep.Type == IMAGE_NS:
55 try:
56 image = Image(BytesIO(archive.read(dep.target)))
57 except OSError:
58 msg = "The image {0} will be removed because it cannot be read".format(dep.target)
59 warn(msg)
60 continue
61 if image.format.upper() == "WMF": # cannot save
62 msg = "{0} image format is not supported so the image is being dropped".format(image.format)
63 warn(msg)
64 continue
65 image.anchor = rel.anchor
66 images.append(image)
67 return charts, images