Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/openpyxl/reader/drawings.py: 17%

46 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1 

2# Copyright (c) 2010-2022 openpyxl 

3 

4 

5from io import BytesIO 

6from warnings import warn 

7 

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 

15 

16 

17def find_images(archive, path): 

18 """ 

19 Given the path to a drawing file extract charts and images 

20 

21 Ingore errors due to unsupported parts of DrawingML 

22 """ 

23 

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 [], [] 

31 

32 rels_path = get_rels_path(path) 

33 deps = [] 

34 if rels_path in archive.namelist(): 

35 deps = get_dependents(archive, rels_path) 

36 

37 charts = [] 

38 for rel in drawing._chart_rels: 

39 cs = get_rel(archive, deps, rel.id, ChartSpace) 

40 chart = read_chart(cs) 

41 chart.anchor = rel.anchor 

42 charts.append(chart) 

43 

44 images = [] 

45 if not PILImage: # Pillow not installed, drop images 

46 return charts, images 

47 

48 for rel in drawing._blip_rels: 

49 dep = deps[rel.embed] 

50 if dep.Type == IMAGE_NS: 

51 try: 

52 image = Image(BytesIO(archive.read(dep.target))) 

53 except OSError: 

54 msg = "The image {0} will be removed because it cannot be read".format(dep.target) 

55 warn(msg) 

56 continue 

57 if image.format.upper() == "WMF": # cannot save 

58 msg = "{0} image format is not supported so the image is being dropped".format(image.format) 

59 warn(msg) 

60 continue 

61 image.anchor = rel.anchor 

62 images.append(image) 

63 return charts, images