text
stringlengths
81
112k
Load the image part collection with all the image parts in package. def _gather_image_parts(self): """Load the image part collection with all the image parts in package.""" for rel in self.iter_rels(): if rel.is_external: continue if rel.reltype != RT.IMAGE: ...
Return |ImagePart| object containing image identified by *image_descriptor*. The image-part is newly created if a matching one is not present in the collection. def get_or_add_image_part(self, image_descriptor): """Return |ImagePart| object containing image identified by *image_descriptor*. ...
Return an |ImagePart| instance newly created from image and appended to the collection. def _add_image_part(self, image): """ Return an |ImagePart| instance newly created from image and appended to the collection. """ partname = self._next_image_partname(image.ext) ...
Return the image part in this collection having a SHA1 hash matching *sha1*, or |None| if not found. def _get_by_sha1(self, sha1): """ Return the image part in this collection having a SHA1 hash matching *sha1*, or |None| if not found. """ for image_part in self._image_p...
The next available image partname, starting from ``/word/media/image1.{ext}`` where unused numbers are reused. The partname is unique by number, without regard to the extension. *ext* does not include the leading period. def _next_image_partname(self, ext): """ The next availabl...
Return HeaderPart or FooterPart object for this section. If this header/footer inherits its content, the part for the prior header/footer is returned; this process continue recursively until a definition is found. If the definition cannot be inherited (because the header/footer belongs to the ...
Return newly-added footer part. def _add_definition(self): """Return newly-added footer part.""" footer_part, rId = self._document_part.add_footer_part() self._sectPr.add_footerReference(self._hdrftr_index, rId) return footer_part
|FooterPart| object containing content of this footer. def _definition(self): """|FooterPart| object containing content of this footer.""" footerReference = self._sectPr.get_footerReference(self._hdrftr_index) return self._document_part.footer_part(footerReference.rId)
Remove footer definition (footer part) associated with this section. def _drop_definition(self): """Remove footer definition (footer part) associated with this section.""" rId = self._sectPr.remove_footerReference(self._hdrftr_index) self._document_part.drop_rel(rId)
True if a footer is defined for this section. def _has_definition(self): """True if a footer is defined for this section.""" footerReference = self._sectPr.get_footerReference(self._hdrftr_index) return False if footerReference is None else True
|_Footer| proxy on prior sectPr element or None if this is first section. def _prior_headerfooter(self): """|_Footer| proxy on prior sectPr element or None if this is first section.""" preceding_sectPr = self._sectPr.preceding_sectPr return ( None if preceding_sectPr is None ...
Return newly-added header part. def _add_definition(self): """Return newly-added header part.""" header_part, rId = self._document_part.add_header_part() self._sectPr.add_headerReference(self._hdrftr_index, rId) return header_part
|HeaderPart| object containing content of this header. def _definition(self): """|HeaderPart| object containing content of this header.""" headerReference = self._sectPr.get_headerReference(self._hdrftr_index) return self._document_part.header_part(headerReference.rId)
Remove header definition associated with this section. def _drop_definition(self): """Remove header definition associated with this section.""" rId = self._sectPr.remove_headerReference(self._hdrftr_index) self._document_part.drop_header_part(rId)
True if a header is explicitly defined for this section. def _has_definition(self): """True if a header is explicitly defined for this section.""" headerReference = self._sectPr.get_headerReference(self._hdrftr_index) return False if headerReference is None else True
|_Header| proxy on prior sectPr element or None if this is first section. def _prior_headerfooter(self): """|_Header| proxy on prior sectPr element or None if this is first section.""" preceding_sectPr = self._sectPr.preceding_sectPr return ( None if preceding_sectPr is None ...
Return a heading paragraph newly added to the end of the document. The heading paragraph will contain *text* and have its paragraph style determined by *level*. If *level* is 0, the style is set to `Title`. If *level* is 1 (or omitted), `Heading 1` is used. Otherwise the style is set to `Headin...
Return newly |Paragraph| object containing only a page break. def add_page_break(self): """Return newly |Paragraph| object containing only a page break.""" paragraph = self.add_paragraph() paragraph.add_run().add_break(WD_BREAK.PAGE) return paragraph
Return a new picture shape added in its own paragraph at the end of the document. The picture contains the image at *image_path_or_stream*, scaled based on *width* and *height*. If neither width nor height is specified, the picture appears at its native size. If only one is specified, it...
Return a |Section| object representing a new section added at the end of the document. The optional *start_type* argument must be a member of the :ref:`WdSectionStart` enumeration, and defaults to ``WD_SECTION.NEW_PAGE`` if not provided. def add_section(self, start_type=WD_SECTION.NEW_PAGE): ...
Add a table having row and column counts of *rows* and *cols* respectively and table style of *style*. *style* may be a paragraph style object or a paragraph style name. If *style* is |None|, the table inherits the default table style of the document. def add_table(self, rows, cols, style=None)...
Return a |Length| object specifying the width of available "writing" space between the margins of the last section of this document. def _block_width(self): """ Return a |Length| object specifying the width of available "writing" space between the margins of the last section of this doc...
The |_Body| instance containing the content for this document. def _body(self): """ The |_Body| instance containing the content for this document. """ if self.__body is None: self.__body = _Body(self._element.body, self) return self.__body
Register *cls* to be constructed when the oxml parser encounters an element with matching *tag*. *tag* is a string of the form ``nspfx:tagroot``, e.g. ``'w:document'``. def register_element_cls(tag, cls): """ Register *cls* to be constructed when the oxml parser encounters an element with matching ...
Return a 'loose' lxml element having the tag specified by *nsptag_str*. *nsptag_str* must contain the standard namespace prefix, e.g. 'a:tbl'. The resulting element is an instance of the custom element class for this tag name if one is defined. A dictionary of attribute values may be provided as *attrs*...
Return an |_IfdEntry| subclass instance containing the value of the directory entry at *offset* in *stream_rdr*. def _IfdEntryFactory(stream_rdr, offset): """ Return an |_IfdEntry| subclass instance containing the value of the directory entry at *offset* in *stream_rdr*. """ ifd_entry_classes =...
Return a |Tiff| instance containing the properties of the TIFF image in *stream*. def from_stream(cls, stream): """ Return a |Tiff| instance containing the properties of the TIFF image in *stream*. """ parser = _TiffParser.parse(stream) px_width = parser.px_widt...
Return an instance of |_TiffParser| containing the properties parsed from the TIFF image in *stream*. def parse(cls, stream): """ Return an instance of |_TiffParser| containing the properties parsed from the TIFF image in *stream*. """ stream_rdr = cls._make_stream_reade...
Return either BIG_ENDIAN or LITTLE_ENDIAN depending on the endian indicator found in the TIFF *stream* header, either 'MM' or 'II'. def _detect_endian(cls, stream): """ Return either BIG_ENDIAN or LITTLE_ENDIAN depending on the endian indicator found in the TIFF *stream* header, either ...
Return the dpi value calculated for *resolution_tag*, which can be either TIFF_TAG.X_RESOLUTION or TIFF_TAG.Y_RESOLUTION. The calculation is based on the values of both that tag and the TIFF_TAG.RESOLUTION_UNIT tag in this parser's |_IfdEntries| instance. def _dpi(self, resolution_tag): ...
Return a |StreamReader| instance with wrapping *stream* and having "endian-ness" determined by the 'MM' or 'II' indicator in the TIFF stream header. def _make_stream_reader(cls, stream): """ Return a |StreamReader| instance with wrapping *stream* and having "endian-ness" determi...
Return a new |_IfdEntries| instance parsed from *stream* starting at *offset*. def from_stream(cls, stream, offset): """ Return a new |_IfdEntries| instance parsed from *stream* starting at *offset*. """ ifd_parser = _IfdParser(stream, offset) entries = dict((e.t...
Generate an |_IfdEntry| instance corresponding to each entry in the directory. def iter_entries(self): """ Generate an |_IfdEntry| instance corresponding to each entry in the directory. """ for idx in range(self._entry_count): dir_entry_offset = self._offset ...
Return an |_IfdEntry| subclass instance containing the tag and value of the tag parsed from *stream_rdr* at *offset*. Note this method is common to all subclasses. Override the ``_parse_value()`` method to provide distinctive behavior based on field type. def from_stream(cls, stream_rdr, offset...
Return the ASCII string parsed from *stream_rdr* at *value_offset*. The length of the string, including a terminating '\x00' (NUL) character, is in *value_count*. def _parse_value(cls, stream_rdr, offset, value_count, value_offset): """ Return the ASCII string parsed from *stream_rdr* a...
Return the short int value contained in the *value_offset* field of this entry. Only supports single values at present. def _parse_value(cls, stream_rdr, offset, value_count, value_offset): """ Return the short int value contained in the *value_offset* field of this entry. Only supports...
Return the long int value contained in the *value_offset* field of this entry. Only supports single values at present. def _parse_value(cls, stream_rdr, offset, value_count, value_offset): """ Return the long int value contained in the *value_offset* field of this entry. Only supports s...
Return the rational (numerator / denominator) value at *value_offset* in *stream_rdr* as a floating-point number. Only supports single values at present. def _parse_value(cls, stream_rdr, offset, value_count, value_offset): """ Return the rational (numerator / denominator) value at *val...
Return (footer_part, rId) pair for newly-created footer part. def add_footer_part(self): """Return (footer_part, rId) pair for newly-created footer part.""" footer_part = FooterPart.new(self.package) rId = self.relate_to(footer_part, RT.FOOTER) return footer_part, rId
Return (header_part, rId) pair for newly-created header part. def add_header_part(self): """Return (header_part, rId) pair for newly-created header part.""" header_part = HeaderPart.new(self.package) rId = self.relate_to(header_part, RT.HEADER) return header_part, rId
A |NumberingPart| object providing access to the numbering definitions for this document. Creates an empty numbering part if one is not present. def numbering_part(self): """ A |NumberingPart| object providing access to the numbering definitions for this document. Creates an emp...
A |SettingsPart| object providing access to the document-level settings for this document. Creates a default settings part if one is not present. def _settings_part(self): """ A |SettingsPart| object providing access to the document-level settings for this document. Creates a de...
Instance of |StylesPart| for this document. Creates an empty styles part if one is not present. def _styles_part(self): """ Instance of |StylesPart| for this document. Creates an empty styles part if one is not present. """ try: return self.part_related_by(RT...
Return string containing the contents of the file at *relpath* relative to this file. def text_of(relpath): """ Return string containing the contents of the file at *relpath* relative to this file. """ thisdir = os.path.dirname(__file__) file_path = os.path.join(thisdir, os.path.normpath(re...
Write a physical package (.pptx file) to *pkg_file* containing *pkg_rels* and *parts* and a content types stream based on the content types of the parts. def write(pkg_file, pkg_rels, parts): """ Write a physical package (.pptx file) to *pkg_file* containing *pkg_rels* and *part...
Write ``[Content_Types].xml`` part to the physical package with an appropriate content type lookup target for each part in *parts*. def _write_content_types_stream(phys_writer, parts): """ Write ``[Content_Types].xml`` part to the physical package with an appropriate content type lookup...
Write the blob of each part in *parts* to the package, along with a rels item for its relationships if and only if it has any. def _write_parts(phys_writer, parts): """ Write the blob of each part in *parts* to the package, along with a rels item for its relationships if and only if it ...
Return content types XML mapping each part in *parts* to the appropriate content type and suitable for storage as ``[Content_Types].xml`` in an OPC package. def from_parts(cls, parts): """ Return content types XML mapping each part in *parts* to the appropriate content type and ...
Add a content type for the part with *partname* and *content_type*, using a default or override as appropriate. def _add_content_type(self, partname, content_type): """ Add a content type for the part with *partname* and *content_type*, using a default or override as appropriate. ...
Return XML form of this content types item, suitable for storage as ``[Content_Types].xml`` in an OPC package. Although the sequence of elements is not strictly significant, as an aid to testing and readability Default elements are sorted by extension and Override elements are sorted by ...
Mixed here meaning it contains ``<w:tab/>``, ``<w:cr/>``, etc. elements. def given_a_run_having_mixed_text_content(context): """ Mixed here meaning it contains ``<w:tab/>``, ``<w:cr/>``, etc. elements. """ r_xml = """\ <w:r %s> <w:t>abc</w:t> <w:tab/> <w:t>def</w:t...
Return `w:sectPr` element for new section added at end of document. The last `w:sectPr` becomes the second-to-last, with the new `w:sectPr` being an exact clone of the previous one, except that all header and footer references are removed (and are therefore now "inherited" from the prior sectio...
Remove all content child elements from this <w:body> element. Leave the <w:sectPr> element if it is present. def clear_content(self): """ Remove all content child elements from this <w:body> element. Leave the <w:sectPr> element if it is present. """ if self.sectPr is no...
Return a |BaseImageHeader| subclass instance that knows how to parse the headers of the image in *stream*. def _ImageHeaderFactory(stream): """ Return a |BaseImageHeader| subclass instance that knows how to parse the headers of the image in *stream*. """ from docx.image import SIGNATURES d...
Return a new |Image| subclass instance parsed from the image binary contained in *blob*. def from_blob(cls, blob): """ Return a new |Image| subclass instance parsed from the image binary contained in *blob*. """ stream = BytesIO(blob) return cls._from_stream(stre...
Return a new |Image| subclass instance loaded from the image file identified by *image_descriptor*, a path or file-like object. def from_file(cls, image_descriptor): """ Return a new |Image| subclass instance loaded from the image file identified by *image_descriptor*, a path or file-li...
Return a (cx, cy) 2-tuple representing the native dimensions of this image scaled by applying the following rules to *width* and *height*. If both *width* and *height* are specified, the return value is (*width*, *height*); no scaling is performed. If only one is specified, it is used to...
Return an instance of the |Image| subclass corresponding to the format of the image in *stream*. def _from_stream(cls, stream, blob, filename=None): """ Return an instance of the |Image| subclass corresponding to the format of the image in *stream*. """ image_header = _I...
Append a run to this paragraph containing *text* and having character style identified by style ID *style*. *text* can contain tab (``\\t``) characters, which are converted to the appropriate XML form for a tab. *text* can also include newline (``\\n``) or carriage return (``\\r``) chara...
Return a newly created paragraph, inserted directly before this paragraph. If *text* is supplied, the new paragraph contains that text in a single run. If *style* is provided, that style is assigned to the new paragraph. def insert_paragraph_before(self, text=None, style=None): """ ...
Read/Write. |_ParagraphStyle| object representing the style assigned to this paragraph. If no explicit style is assigned to this paragraph, its value is the default paragraph style for the document. A paragraph style name can be assigned in lieu of a paragraph style object. Assigning |No...
String formed by concatenating the text of each run in the paragraph. Tabs and line breaks in the XML are mapped to ``\\t`` and ``\\n`` characters respectively. Assigning text to this property causes all existing paragraph content to be replaced with a single run containing the assigned...
Return a newly created paragraph, inserted directly before this paragraph. def _insert_paragraph_before(self): """ Return a newly created paragraph, inserted directly before this paragraph. """ p = self._p.add_p_before() return Paragraph(p, self._parent)
Serialize *element* to human-readable XML suitable for tests. No XML declaration. def serialize_for_reading(element): """ Serialize *element* to human-readable XML suitable for tests. No XML declaration. """ xml = etree.tostring(element, encoding='unicode', pretty_print=True) return XmlStri...
Return a sequence of attribute strings parsed from *attrs*. Each attribute string is stripped of whitespace on both ends. def _attr_seq(self, attrs): """ Return a sequence of attribute strings parsed from *attrs*. Each attribute string is stripped of whitespace on both ends. """...
Return True if the element in *line_2* is XML equivalent to the element in *line*. def _eq_elm_strs(self, line, line_2): """ Return True if the element in *line_2* is XML equivalent to the element in *line*. """ front, attrs, close, text = self._parse_line(line) ...
Return front, attrs, close, text 4-tuple result of parsing XML element string *line*. def _parse_line(cls, line): """ Return front, attrs, close, text 4-tuple result of parsing XML element string *line*. """ match = cls._xml_elm_line_patt.match(line) front, attrs...
Add the appropriate methods to *element_cls*. def populate_class_members(self, element_cls, prop_name): """ Add the appropriate methods to *element_cls*. """ self._element_cls = element_cls self._prop_name = prop_name self._add_attr_property()
Add a read/write ``{prop_name}`` property to the element class that returns the interpreted value of this attribute on access and changes the attribute value to its ST_* counterpart on assignment. def _add_attr_property(self): """ Add a read/write ``{prop_name}`` property to the element...
Return a function object suitable for the "get" side of the attribute property descriptor. def _getter(self): """ Return a function object suitable for the "get" side of the attribute property descriptor. """ def get_attr_value(obj): attr_str_value = obj.get(...
Return a function object suitable for the "set" side of the attribute property descriptor. def _setter(self): """ Return a function object suitable for the "set" side of the attribute property descriptor. """ def set_attr_value(obj, value): if value is None o...
Return a function object suitable for the "get" side of the attribute property descriptor. def _getter(self): """ Return a function object suitable for the "get" side of the attribute property descriptor. """ def get_attr_value(obj): attr_str_value = obj.get(...
Return a function object suitable for the "set" side of the attribute property descriptor. def _setter(self): """ Return a function object suitable for the "set" side of the attribute property descriptor. """ def set_attr_value(obj, value): str_value = self._...
Add an ``_add_x()`` method to the element class for this child element. def _add_adder(self): """ Add an ``_add_x()`` method to the element class for this child element. """ def _add_child(obj, **attrs): new_method = getattr(obj, self._new_method_name) ...
Add a ``_new_{prop_name}()`` method to the element class that creates a new, empty element of the correct type, having no attributes. def _add_creator(self): """ Add a ``_new_{prop_name}()`` method to the element class that creates a new, empty element of the correct type, having no att...
Add a read-only ``{prop_name}`` property to the element class for this child element. def _add_getter(self): """ Add a read-only ``{prop_name}`` property to the element class for this child element. """ property_ = property(self._getter, None, None) # assign unco...
Add an ``_insert_x()`` method to the element class for this child element. def _add_inserter(self): """ Add an ``_insert_x()`` method to the element class for this child element. """ def _insert_child(obj, child): obj.insert_element_before(child, *self._succe...
Add a read-only ``{prop_name}_lst`` property to the element class to retrieve a list of child elements matching this type. def _add_list_getter(self): """ Add a read-only ``{prop_name}_lst`` property to the element class to retrieve a list of child elements matching this type. "...
Add a public ``add_x()`` method to the parent element class. def _add_public_adder(self): """ Add a public ``add_x()`` method to the parent element class. """ def add_child(obj): private_add_method = getattr(obj, self._add_method_name) child = private_add_method(...
Add *method* to the target class as *name*, unless *name* is already defined on the class. def _add_to_class(self, name, method): """ Add *method* to the target class as *name*, unless *name* is already defined on the class. """ if hasattr(self._element_cls, name): ...
Return a function object suitable for the "get" side of the property descriptor. This default getter returns the child element with matching tag name or |None| if not present. def _getter(self): """ Return a function object suitable for the "get" side of the property descriptor....
Return a function object suitable for the "get" side of a list property descriptor. def _list_getter(self): """ Return a function object suitable for the "get" side of a list property descriptor. """ def get_child_element_list(obj): return obj.findall(qn(self...
Add the appropriate methods to *element_cls*. def populate_class_members( self, element_cls, group_prop_name, successors): """ Add the appropriate methods to *element_cls*. """ self._element_cls = element_cls self._group_prop_name = group_prop_name self._succ...
Add a ``get_or_change_to_x()`` method to the element class for this child element. def _add_get_or_change_to_method(self): """ Add a ``get_or_change_to_x()`` method to the element class for this child element. """ def get_or_change_to_child(obj): child = geta...
Calculate property name from tag name, e.g. a:schemeClr -> schemeClr. def _prop_name(self): """ Calculate property name from tag name, e.g. a:schemeClr -> schemeClr. """ if ':' in self._nsptagname: start = self._nsptagname.index(':') + 1 else: start = 0 ...
Add the appropriate methods to *element_cls*. def populate_class_members(self, element_cls, prop_name): """ Add the appropriate methods to *element_cls*. """ super(OneAndOnlyOne, self).populate_class_members( element_cls, prop_name ) self._add_getter()
Add the appropriate methods to *element_cls*. def populate_class_members(self, element_cls, prop_name): """ Add the appropriate methods to *element_cls*. """ super(OneOrMore, self).populate_class_members( element_cls, prop_name ) self._add_list_getter() ...
Add the appropriate methods to *element_cls*. def populate_class_members(self, element_cls, prop_name): """ Add the appropriate methods to *element_cls*. """ super(ZeroOrOne, self).populate_class_members(element_cls, prop_name) self._add_getter() self._add_creator() ...
Add a ``get_or_add_x()`` method to the element class for this child element. def _add_get_or_adder(self): """ Add a ``get_or_add_x()`` method to the element class for this child element. """ def get_or_add_child(obj): child = getattr(obj, self._prop_name) ...
Add a ``_remove_x()`` method to the element class for this child element. def _add_remover(self): """ Add a ``_remove_x()`` method to the element class for this child element. """ def _remove_child(obj): obj.remove_all(self._nsptagname) _remove_child....
Add the appropriate methods to *element_cls*. def populate_class_members(self, element_cls, prop_name): """ Add the appropriate methods to *element_cls*. """ super(ZeroOrOneChoice, self).populate_class_members( element_cls, prop_name ) self._add_choice_getter...
Add a read-only ``{prop_name}`` property to the element class that returns the present member of this group, or |None| if none are present. def _add_choice_getter(self): """ Add a read-only ``{prop_name}`` property to the element class that returns the present member of this gro...
Add a ``_remove_eg_x()`` method to the element class for this choice group. def _add_group_remover(self): """ Add a ``_remove_eg_x()`` method to the element class for this choice group. """ def _remove_choice_group(obj): for tagname in self._member_nsptagname...
Return a function object suitable for the "get" side of the property descriptor. def _choice_getter(self): """ Return a function object suitable for the "get" side of the property descriptor. """ def get_group_member_element(obj): return obj.first_child_found...
Return the first child found with tag in *tagnames*, or None if not found. def first_child_found_in(self, *tagnames): """ Return the first child found with tag in *tagnames*, or None if not found. """ for tagname in tagnames: child = self.find(qn(tagname)) ...
Remove all child elements whose tagname (e.g. 'a:p') appears in *tagnames*. def remove_all(self, *tagnames): """ Remove all child elements whose tagname (e.g. 'a:p') appears in *tagnames*. """ for tagname in tagnames: matching = self.findall(qn(tagname)) ...
Override of ``lxml`` _Element.xpath() method to provide standard Open XML namespace mapping (``nsmap``) in centralized location. def xpath(self, xpath_str): """ Override of ``lxml`` _Element.xpath() method to provide standard Open XML namespace mapping (``nsmap``) in centralized locatio...
An |RGBColor| value or |None| if no RGB color is specified. When :attr:`type` is `MSO_COLOR_TYPE.RGB`, the value of this property will always be an |RGBColor| value. It may also be an |RGBColor| value if :attr:`type` is `MSO_COLOR_TYPE.THEME`, as Word writes the current value of a theme...
A member of :ref:`MsoThemeColorIndex` or |None| if no theme color is specified. When :attr:`type` is `MSO_COLOR_TYPE.THEME`, the value of this property will always be a member of :ref:`MsoThemeColorIndex`. When :attr:`type` has any other value, the value of this property is |None|. ...
Read-only. A member of :ref:`MsoColorType`, one of RGB, THEME, or AUTO, corresponding to the way this color is defined. Its value is |None| if no color is applied at this level, which causes the effective color to be inherited from the style hierarchy. def type(self): """ Read-o...