|
| 1 | +from ..oxml.shape import CT_Inline |
| 2 | +from ..opc.constants import RELATIONSHIP_TYPE as RT |
| 3 | +from ..opc.part import XmlPart |
| 4 | +from ..shape import InlineShapes |
| 5 | +from ..shared import lazyproperty |
| 6 | +from .styles import StylesPart |
| 7 | + |
| 8 | + |
| 9 | +class HeaderPart(XmlPart): |
| 10 | + # COPYPASTA FROM DOCUMENT PART BELOW THIS POINT |
| 11 | + # TODO ABSTRACT? |
| 12 | + |
| 13 | + # @lazyproperty |
| 14 | + # def inline_shapes(self): |
| 15 | + # """ |
| 16 | + # The |InlineShapes| instance containing the inline shapes in the |
| 17 | + # document. |
| 18 | + # """ |
| 19 | + # return InlineShapes(self._element.body, self) |
| 20 | + |
| 21 | + @property |
| 22 | + def next_id(self): |
| 23 | + """ |
| 24 | + The next available positive integer id value in this document. Gaps |
| 25 | + in id sequence are filled. The id attribute value is unique in the |
| 26 | + document, without regard to the element type it appears on. |
| 27 | + """ |
| 28 | + id_str_lst = self._element.xpath('//@id') |
| 29 | + used_ids = [int(id_str) for id_str in id_str_lst if id_str.isdigit()] |
| 30 | + for n in range(1, len(used_ids)+2): |
| 31 | + if n not in used_ids: |
| 32 | + return n |
| 33 | + |
| 34 | + def get_or_add_image(self, image_descriptor): |
| 35 | + """ |
| 36 | + Return an (rId, image) 2-tuple for the image identified by |
| 37 | + *image_descriptor*. *image* is an |Image| instance providing access |
| 38 | + to the properties of the image, such as dimensions and image type. |
| 39 | + *rId* is the key for the relationship between this document part and |
| 40 | + the image part, reused if already present, newly created if not. |
| 41 | + """ |
| 42 | + image_part = self._package.image_parts.get_or_add_image_part( |
| 43 | + image_descriptor |
| 44 | + ) |
| 45 | + rId = self.relate_to(image_part, RT.IMAGE) |
| 46 | + return rId, image_part.image |
| 47 | + |
| 48 | + def new_pic_inline(self, image_descriptor, width, height): |
| 49 | + """ |
| 50 | + Return a newly-created `w:inline` element containing the image |
| 51 | + specified by *image_descriptor* and scaled based on the values of |
| 52 | + *width* and *height*. |
| 53 | + """ |
| 54 | + rId, image = self.get_or_add_image(image_descriptor) |
| 55 | + cx, cy = image.scaled_dimensions(width, height) |
| 56 | + shape_id, filename = self.next_id, image.filename |
| 57 | + return CT_Inline.new_pic_inline(shape_id, rId, filename, cx, cy) |
| 58 | + |
| 59 | + def get_style(self, style_id, style_type): |
| 60 | + """ |
| 61 | + Return the style in this document matching *style_id*. Returns the |
| 62 | + default style for *style_type* if *style_id* is |None| or does not |
| 63 | + match a defined style of *style_type*. |
| 64 | + """ |
| 65 | + return self.styles.get_by_id(style_id, style_type) |
| 66 | + |
| 67 | + def get_style_id(self, style_or_name, style_type): |
| 68 | + """ |
| 69 | + Return the style_id (|str|) of the style of *style_type* matching |
| 70 | + *style_or_name*. Returns |None| if the style resolves to the default |
| 71 | + style for *style_type* or if *style_or_name* is itself |None|. Raises |
| 72 | + if *style_or_name* is a style of the wrong type or names a style not |
| 73 | + present in the document. |
| 74 | + """ |
| 75 | + return self.styles.get_style_id(style_or_name, style_type) |
| 76 | + |
| 77 | + @lazyproperty |
| 78 | + def inline_shapes(self): |
| 79 | + """ |
| 80 | + The |InlineShapes| instance containing the inline shapes in the |
| 81 | + document. |
| 82 | + """ |
| 83 | + return InlineShapes(self._element.body, self) |
| 84 | + |
| 85 | + @property |
| 86 | + def styles(self): |
| 87 | + """ |
| 88 | + A |Styles| object providing access to the styles in the styles part |
| 89 | + of this document. |
| 90 | + """ |
| 91 | + return self._styles_part.styles |
| 92 | + |
| 93 | + @property |
| 94 | + def _styles_part(self): |
| 95 | + """ |
| 96 | + Instance of |StylesPart| for this document. Creates an empty styles |
| 97 | + part if one is not present. |
| 98 | + """ |
| 99 | + # HACK |
| 100 | + # one styles to rule them all |
| 101 | + document = self.package.main_document_part |
| 102 | + try: |
| 103 | + return document.part_related_by(RT.STYLES) |
| 104 | + except KeyError: |
| 105 | + styles_part = StylesPart.default(self.package) |
| 106 | + document.relate_to(styles_part, RT.STYLES) |
| 107 | + return styles_part |
| 108 | + |
| 109 | + |
| 110 | +class FooterPart(HeaderPart): |
| 111 | + # identical to HeaderPart for now |
| 112 | + pass |
0 commit comments