text
stringlengths
81
112k
Return newly created footer part. def new(cls, package): """Return newly created footer part.""" partname = package.next_partname("/word/footer%d.xml") content_type = CT.WML_FOOTER element = parse_xml(cls._default_footer_xml()) return cls(partname, content_type, element, package...
Return newly created header part. def new(cls, package): """Return newly created header part.""" partname = package.next_partname("/word/header%d.xml") content_type = CT.WML_HEADER element = parse_xml(cls._default_header_xml()) return cls(partname, content_type, element, package...
Return a newly created styles part, containing a default set of elements. def default(cls, package): """ Return a newly created styles part, containing a default set of elements. """ partname = PackURI('/word/styles.xml') content_type = CT.WML_STYLES elem...
Return a bytestream containing XML for a default styles part. def _default_styles_xml(cls): """ Return a bytestream containing XML for a default styles part. """ path = os.path.join( os.path.split(__file__)[0], '..', 'templates', 'default-styles.xml' ) ...
Return newly added CT_HdrFtrRef element of *type_* with *rId*. The element tag is `w:footerReference`. def add_footerReference(self, type_, rId): """Return newly added CT_HdrFtrRef element of *type_* with *rId*. The element tag is `w:footerReference`. """ footerReference = sel...
Return newly added CT_HdrFtrRef element of *type_* with *rId*. The element tag is `w:headerReference`. def add_headerReference(self, type_, rId): """Return newly added CT_HdrFtrRef element of *type_* with *rId*. The element tag is `w:headerReference`. """ headerReference = sel...
Return footerReference element of *type_* or None if not present. def get_footerReference(self, type_): """Return footerReference element of *type_* or None if not present.""" path = "./w:footerReference[@w:type='%s']" % WD_HEADER_FOOTER.to_xml(type_) footerReferences = self.xpath(path) ...
Return headerReference element of *type_* or None if not present. def get_headerReference(self, type_): """Return headerReference element of *type_* or None if not present.""" matching_headerReferences = self.xpath( "./w:headerReference[@w:type='%s']" % WD_HEADER_FOOTER.to_xml(type_) ...
The member of the ``WD_ORIENTATION`` enumeration corresponding to the value of the ``orient`` attribute of the ``<w:pgSz>`` child element, or ``WD_ORIENTATION.PORTRAIT`` if not present. def orientation(self): """ The member of the ``WD_ORIENTATION`` enumeration corresponding to the ...
Return rId of w:footerReference child of *type_* after removing it. def remove_footerReference(self, type_): """Return rId of w:footerReference child of *type_* after removing it.""" footerReference = self.get_footerReference(type_) rId = footerReference.rId self.remove(footerReference)...
Return rId of w:headerReference child of *type_* after removing it. def remove_headerReference(self, type_): """Return rId of w:headerReference child of *type_* after removing it.""" headerReference = self.get_headerReference(type_) rId = headerReference.rId self.remove(headerReference)...
The member of the ``WD_SECTION_START`` enumeration corresponding to the value of the ``val`` attribute of the ``<w:type>`` child element, or ``WD_SECTION_START.NEW_PAGE`` if not present. def start_type(self): """ The member of the ``WD_SECTION_START`` enumeration corresponding to ...
Return the boolean value of the attribute having *attr_name*, or |False| if not present. def bool_prop(self, attr_name): """ Return the boolean value of the attribute having *attr_name*, or |False| if not present. """ value = getattr(self, attr_name) if value is ...
Sibling CT_Style element this style is based on or |None| if no base style or base style not found. def base_style(self): """ Sibling CT_Style element this style is based on or |None| if no base style or base style not found. """ basedOn = self.basedOn if basedOn...
Sibling CT_Style element identified by the value of `w:name/@w:val` or |None| if no value is present or no style with that style id is found. def next_style(self): """ Sibling CT_Style element identified by the value of `w:name/@w:val` or |None| if no value is present or no styl...
Return a newly added `w:style` element having *name* and *style_type*. `w:style/@customStyle` is set based on the value of *builtin*. def add_style_of_type(self, name, style_type, builtin): """ Return a newly added `w:style` element having *name* and *style_type*. `w:style/@cust...
Return `w:style[@w:type="*{style_type}*][-1]` or |None| if not found. def default_for(self, style_type): """ Return `w:style[@w:type="*{style_type}*][-1]` or |None| if not found. """ default_styles_for_type = [ s for s in self._iter_styles() if s.type == style_ty...
Add a break element of *break_type* to this run. *break_type* can take the values `WD_BREAK.LINE`, `WD_BREAK.PAGE`, and `WD_BREAK.COLUMN` where `WD_BREAK` is imported from `docx.enum.text`. *break_type* defaults to `WD_BREAK.LINE`. def add_break(self, break_type=WD_BREAK.LINE): """ ...
Return an |InlineShape| instance containing the image identified by *image_path_or_stream*, added to the end of this run. *image_path_or_stream* can be a path (a string) or a file-like object containing a binary image. If neither width nor height is specified, the picture appears at its ...
Returns a newly appended |_Text| object (corresponding to a new ``<w:t>`` child element) to the run, containing *text*. Compare with the possibly more friendly approach of assigning text to the :attr:`Run.text` property. def add_text(self, text): """ Returns a newly appended |_T...
Read/write. A |_CharacterStyle| object representing the character style applied to this run. The default character style for the document (often `Default Character Font`) is returned if the run has no directly-applied character style. Setting this property to |None| removes any directly-...
A |Length| value calculated from the values of `w:ind/@w:firstLine` and `w:ind/@w:hanging`. Returns |None| if the `w:ind` child is not present. def first_line_indent(self): """ A |Length| value calculated from the values of `w:ind/@w:firstLine` and `w:ind/@w:hanging`. Returns |N...
The value of `w:spacing/@w:lineRule` as a member of the :ref:`WdLineSpacing` enumeration. Only the `MULTIPLE`, `EXACTLY`, and `AT_LEAST` members are used. It is the responsibility of the client to calculate the use of `SINGLE`, `DOUBLE`, and `MULTIPLE` based on the value of `w:spacing/@w...
Set val attribute of <w:pStyle> child element to *style*, adding a new element if necessary. If *style* is |None|, remove the <w:pStyle> element if present. def style(self, style): """ Set val attribute of <w:pStyle> child element to *style*, adding a new element if necessary. I...
Insert a newly created `w:tab` child element in *pos* order. def insert_tab_in_order(self, pos, align, leader): """ Insert a newly created `w:tab` child element in *pos* order. """ new_tab = self._new_tab() new_tab.pos, new_tab.val, new_tab.leader = pos, align, leader fo...
Return a new ``<wp:inline>`` element populated with the values passed as parameters. def new(cls, cx, cy, shape_id, pic): """ Return a new ``<wp:inline>`` element populated with the values passed as parameters. """ inline = parse_xml(cls._inline_xml()) inline.ext...
Return a new `wp:inline` element containing the `pic:pic` element specified by the argument values. def new_pic_inline(cls, shape_id, rId, filename, cx, cy): """ Return a new `wp:inline` element containing the `pic:pic` element specified by the argument values. """ pic_i...
Return a new ``<pic:pic>`` element populated with the minimal contents required to define a viable picture element, based on the values passed as parameters. def new(cls, pic_id, filename, rId, cx, cy): """ Return a new ``<pic:pic>`` element populated with the minimal contents r...
Return a newly added style object of *style_type* and identified by *name*. A builtin style can be defined by passing True for the optional *builtin* argument. def add_style(self, name, style_type, builtin=False): """ Return a newly added style object of *style_type* and identified ...
Return the default style for *style_type* or |None| if no default is defined for that type (not common). def default(self, style_type): """ Return the default style for *style_type* or |None| if no default is defined for that type (not common). """ style = self._element....
Return the style of *style_type* matching *style_id*. Returns the default for *style_type* if *style_id* is not found or is |None|, or if the style having *style_id* is not of *style_type*. def get_by_id(self, style_id, style_type): """Return the style of *style_type* matching *style_id*. ...
Return the id of the style corresponding to *style_or_name*, or |None| if *style_or_name* is |None|. If *style_or_name* is not a style object, the style is looked up using *style_or_name* as a style name, raising |ValueError| if no style with that name is defined. Raises |ValueError| if ...
Return the style of *style_type* matching *style_id*. Returns the default for *style_type* if *style_id* is not found or if the style having *style_id* is not of *style_type*. def _get_by_id(self, style_id, style_type): """ Return the style of *style_type* matching *style_id*. Returns t...
Return the id of *style*, or |None| if it is the default style of *style_type*. Raises |ValueError| if style is not of *style_type*. def _get_style_id_from_style(self, style, style_type): """ Return the id of *style*, or |None| if it is the default style of *style_type*. Raises |ValueEr...
Return a new instance from an RGB color hex string like ``'3C2F80'``. def from_string(cls, rgb_hex_str): """ Return a new instance from an RGB color hex string like ``'3C2F80'``. """ r = int(rgb_hex_str[:2], 16) g = int(rgb_hex_str[2:4], 16) b = int(rgb_hex_str[4:], 16) ...
Generate exactly one reference to each relationship in the package by performing a depth-first traversal of the rels graph. def iter_rels(self): """ Generate exactly one reference to each relationship in the package by performing a depth-first traversal of the rels graph. """ ...
Generate exactly one reference to each of the parts in the package by performing a depth-first traversal of the rels graph. def iter_parts(self): """ Generate exactly one reference to each of the parts in the package by performing a depth-first traversal of the rels graph. """ ...
Return newly added |_Relationship| instance of *reltype* between this part and *target* with key *rId*. Target mode is set to ``RTM.EXTERNAL`` if *is_external* is |True|. Intended for use during load from a serialized package, where the rId is well known. Other methods exist for adding a...
Return a |PackURI| instance representing partname matching *template*. The returned part-name has the next available numeric suffix to distinguish it from other parts of its type. *template* is a printf (%)-style template string containing a single replacement item, a '%d' to be used to insert ...
Return an |OpcPackage| instance loaded with the contents of *pkg_file*. def open(cls, pkg_file): """ Return an |OpcPackage| instance loaded with the contents of *pkg_file*. """ pkg_reader = PackageReader.from_file(pkg_file) package = cls() Unmarshaller.un...
Return rId key of relationship to *part*, from the existing relationship if there is one, otherwise a newly created one. def relate_to(self, part, reltype): """ Return rId key of relationship to *part*, from the existing relationship if there is one, otherwise a newly created one. ...
Save this package to *pkg_file*, where *file* can be either a path to a file (a string) or a file-like object. def save(self, pkg_file): """ Save this package to *pkg_file*, where *file* can be either a path to a file (a string) or a file-like object. """ for part in sel...
|CorePropertiesPart| object related to this package. Creates a default core properties part if one is not present (not common). def _core_properties_part(self): """ |CorePropertiesPart| object related to this package. Creates a default core properties part if one is not present (not com...
Construct graph of parts and realized relationships based on the contents of *pkg_reader*, delegating construction of each part to *part_factory*. Package relationships are added to *pkg*. def unmarshal(pkg_reader, package, part_factory): """ Construct graph of parts and realized relati...
Return a dictionary of |Part| instances unmarshalled from *pkg_reader*, keyed by partname. Side-effect is that each part in *pkg_reader* is constructed using *part_factory*. def _unmarshal_parts(pkg_reader, package, part_factory): """ Return a dictionary of |Part| instances unmarshalled...
Add a relationship to the source object corresponding to each of the relationships in *pkg_reader* with its target_part set to the actual target part in *parts*. def _unmarshal_relationships(pkg_reader, package, parts): """ Add a relationship to the source object corresponding to each o...
Return a newly created settings part, containing a default `w:settings` element tree. def default(cls, package): """ Return a newly created settings part, containing a default `w:settings` element tree. """ partname = PackURI('/word/settings.xml') content_type = ...
Return a newly added |_LatentStyle| object to override the inherited defaults defined in this latent styles object for the built-in style having *name*. def add_latent_style(self, name): """ Return a newly added |_LatentStyle| object to override the inherited defaults defined in...
Return rId key of relationship of *reltype* to *target*, from an existing relationship if there is one, otherwise a newly created one. def relate_to(self, target, reltype, is_external=False): """ Return rId key of relationship of *reltype* to *target*, from an existing relationship if t...
Return the count of references in this part's XML to the relationship identified by *rId*. def _rel_ref_count(self, rId): """ Return the count of references in this part's XML to the relationship identified by *rId*. """ rIds = self._element.xpath('//@r:id') retu...
Return the custom part class registered for *content_type*, or the default part class if no custom class is registered for *content_type*. def _part_cls_for(cls, content_type): """ Return the custom part class registered for *content_type*, or the default part class if no custom...
Return a newly added ``<w:t>`` element containing *text*. def add_t(self, text): """ Return a newly added ``<w:t>`` element containing *text*. """ t = self._add_t(text=text) if len(text.strip()) < len(text): t.set(qn('xml:space'), 'preserve') return t
Return a newly appended ``CT_Drawing`` (``<w:drawing>``) child element having *inline_or_anchor* as its child. def add_drawing(self, inline_or_anchor): """ Return a newly appended ``CT_Drawing`` (``<w:drawing>``) child element having *inline_or_anchor* as its child. """ ...
Remove all child elements except the ``<w:rPr>`` element if present. def clear_content(self): """ Remove all child elements except the ``<w:rPr>`` element if present. """ content_child_elms = self[1:] if self.rPr is not None else self[:] for child in content_child_elms: ...
A string representing the textual content of this run, with content child elements like ``<w:tab/>`` translated to their Python equivalent. def text(self): """ A string representing the textual content of this run, with content child elements like ``<w:tab/>`` translated to thei...
Create a "one-shot" ``_RunContentAppender`` instance and use it to append the run content elements corresponding to *text* to the ``<w:r>`` element *r*. def append_to_run_from_text(cls, r, text): """ Create a "one-shot" ``_RunContentAppender`` instance and use it to append the r...
Append the run content elements corresponding to *text* to the ``<w:r>`` element of this instance. def add_text(self, text): """ Append the run content elements corresponding to *text* to the ``<w:r>`` element of this instance. """ for char in text: self.add_...
Process the next character of input through the translation finite state maching (FSM). There are two possible states, buffer pending and not pending, but those are hidden behind the ``.flush()`` method which must be called at the end of text to ensure any pending ``<w:t>`` element is wr...
Return a new |CorePropertiesPart| object initialized with default values for its base properties. def default(cls, package): """ Return a new |CorePropertiesPart| object initialized with default values for its base properties. """ core_properties_part = cls._new(package)...
Return the value of boolean child of `w:rPr` having *name*. def _get_bool_prop(self, name): """ Return the value of boolean child of `w:rPr` having *name*. """ rPr = self._element.rPr if rPr is None: return None return rPr._get_bool_val(name)
Assign *value* to the boolean child *name* of `w:rPr`. def _set_bool_prop(self, name, value): """ Assign *value* to the boolean child *name* of `w:rPr`. """ rPr = self._element.get_or_add_rPr() rPr._set_bool_val(name, value)
Return a |_Column| object of *width*, newly added rightmost to the table. def add_column(self, width): """ Return a |_Column| object of *width*, newly added rightmost to the table. """ tblGrid = self._tbl.tblGrid gridCol = tblGrid.add_gridCol() gridCol.w ...
Return a |_Row| instance, newly added bottom-most to the table. def add_row(self): """ Return a |_Row| instance, newly added bottom-most to the table. """ tbl = self._tbl tr = tbl.add_tr() for gridCol in tbl.tblGrid.gridCol_lst: tc = tr.add_tc() t...
Return |_Cell| instance correponding to table cell at *row_idx*, *col_idx* intersection, where (0, 0) is the top, left-most cell. def cell(self, row_idx, col_idx): """ Return |_Cell| instance correponding to table cell at *row_idx*, *col_idx* intersection, where (0, 0) is the top, left-...
Sequence of cells in the column at *column_idx* in this table. def column_cells(self, column_idx): """ Sequence of cells in the column at *column_idx* in this table. """ cells = self._cells idxs = range(column_idx, len(cells), self._column_count) return [cells[idx] for i...
Sequence of cells in the row at *row_idx* in this table. def row_cells(self, row_idx): """ Sequence of cells in the row at *row_idx* in this table. """ column_count = self._column_count start = row_idx * column_count end = start + column_count return self._cells[...
Read/write. A |_TableStyle| object representing the style applied to this table. The default table style for the document (often `Normal Table`) is returned if the table has no directly-applied style. Assigning |None| to this property removes any directly-applied table style causing it t...
A sequence of |_Cell| objects, one for each cell of the layout grid. If the table contains a span, one or more |_Cell| object references are repeated. def _cells(self): """ A sequence of |_Cell| objects, one for each cell of the layout grid. If the table contains a span, one or ...
Return a paragraph newly added to the end of the content in this cell. If present, *text* is added to the paragraph in a single run. If specified, the paragraph style *style* is applied. If *style* is not specified or is |None|, the result is as though the 'Normal' style was applied. Not...
Return a table newly added to this cell after any existing cell content, having *rows* rows and *cols* columns. An empty paragraph is added after the table because Word requires a paragraph element as the last element in every cell. def add_table(self, rows, cols): """ Return a ...
Return a merged cell created by spanning the rectangular region having this cell and *other_cell* as diagonal corners. Raises |InvalidSpanError| if the cells do not define a rectangular region. def merge(self, other_cell): """ Return a merged cell created by spanning the rectangular reg...
Write-only. Set entire contents of cell to the string *text*. Any existing content or revisions are replaced. def text(self, text): """ Write-only. Set entire contents of cell to the string *text*. Any existing content or revisions are replaced. """ tc = self._tc ...
Return (rId, image) pair for image identified by *image_descriptor*. *rId* is the str key (often like "rId7") for the relationship between this story part and the image part, reused if already present, newly created if not. *image* is an |Image| instance providing access to the properties of th...
Return a newly-created `w:inline` element. The element contains the image specified by *image_descriptor* and is scaled based on the values of *width* and *height*. def new_pic_inline(self, image_descriptor, width, height): """Return a newly-created `w:inline` element. The element con...
Next available positive integer id value in this story XML document. The value is determined by incrementing the maximum existing id value. Gaps in the existing id sequence are not filled. The id attribute value is unique in the document, without regard to the element type it appears on. def n...
Return |_Marker| or subclass instance appropriate for marker at *offset* in *stream* having *marker_code*. def _MarkerFactory(marker_code, stream, offset): """ Return |_Marker| or subclass instance appropriate for marker at *offset* in *stream* having *marker_code*. """ if marker_code == JPEG_M...
Return |Exif| instance having header properties parsed from Exif image in *stream*. def from_stream(cls, stream): """ Return |Exif| instance having header properties parsed from Exif image in *stream*. """ markers = _JfifMarkers.from_stream(stream) # print('\n%s'...
Return a |_JfifMarkers| instance containing a |_JfifMarker| subclass instance for each marker in *stream*. def from_stream(cls, stream): """ Return a |_JfifMarkers| instance containing a |_JfifMarker| subclass instance for each marker in *stream*. """ marker_parser = _Ma...
First APP0 marker in image markers. def app0(self): """ First APP0 marker in image markers. """ for m in self._markers: if m.marker_code == JPEG_MARKER_CODE.APP0: return m raise KeyError('no APP0 marker in image')
First APP1 marker in image markers. def app1(self): """ First APP1 marker in image markers. """ for m in self._markers: if m.marker_code == JPEG_MARKER_CODE.APP1: return m raise KeyError('no APP1 marker in image')
First start of frame (SOFn) marker in this sequence. def sof(self): """ First start of frame (SOFn) marker in this sequence. """ for m in self._markers: if m.marker_code in JPEG_MARKER_CODE.SOF_MARKER_CODES: return m raise KeyError('no start of frame ...
Generate a (marker_code, segment_offset) 2-tuple for each marker in the JPEG *stream*, in the order they occur in the stream. def iter_markers(self): """ Generate a (marker_code, segment_offset) 2-tuple for each marker in the JPEG *stream*, in the order they occur in the stream. ...
Return a (marker_code, segment_offset) 2-tuple identifying and locating the first marker in *stream* occuring after offset *start*. The returned *segment_offset* points to the position immediately following the 2-byte marker code, the start of the marker segment, for those markers that h...
Return an offset, byte 2-tuple for the next byte in *stream* that is not '\xFF', starting with the byte at offset *start*. If the byte at offset *start* is not '\xFF', *start* and the returned *offset* will be the same. def _next_non_ff_byte(self, start): """ Return an offset, b...
Return the offset of the next '\xFF' byte in *stream* starting with the byte at offset *start*. Returns *start* if the byte at that offset is a hex 255; it does not necessarily advance in the stream. def _offset_of_next_ff_byte(self, start): """ Return the offset of the next '\xFF' byte...
Return a generic |_Marker| instance for the marker at *offset* in *stream* having *marker_code*. def from_stream(cls, stream, marker_code, offset): """ Return a generic |_Marker| instance for the marker at *offset* in *stream* having *marker_code*. """ if JPEG_MARKER_COD...
Return dots per inch corresponding to *density* value. def _dpi(self, density): """ Return dots per inch corresponding to *density* value. """ if self._density_units == 1: dpi = density elif self._density_units == 2: dpi = int(round(density * 2.54)) ...
Return an |_App0Marker| instance for the APP0 marker at *offset* in *stream*. def from_stream(cls, stream, marker_code, offset): """ Return an |_App0Marker| instance for the APP0 marker at *offset* in *stream*. """ # field off type notes # ------...
Extract the horizontal and vertical dots-per-inch value from the APP1 header at *offset* in *stream*. def from_stream(cls, stream, marker_code, offset): """ Extract the horizontal and vertical dots-per-inch value from the APP1 header at *offset* in *stream*. """ # field ...
Return True if the APP1 segment at *offset* in *stream* is NOT an Exif segment, as determined by the ``'Exif\x00\x00'`` signature at offset 2 in the segment. def _is_non_Exif_APP1_segment(cls, stream, offset): """ Return True if the APP1 segment at *offset* in *stream* is NOT an ...
Return a |Tiff| instance parsed from the Exif APP1 segment of *segment_length* at *offset* in *stream*. def _tiff_from_exif_segment(cls, stream, offset, segment_length): """ Return a |Tiff| instance parsed from the Exif APP1 segment of *segment_length* at *offset* in *stream*. "...
Return an |_SofMarker| instance for the SOFn marker at *offset* in stream. def from_stream(cls, stream, marker_code, offset): """ Return an |_SofMarker| instance for the SOFn marker at *offset* in stream. """ # field off type notes # ----------...
The ``<w:tc>`` element appearing at grid column *idx*. Raises |ValueError| if no ``w:tc`` element begins at that grid column. def tc_at_grid_col(self, idx): """ The ``<w:tc>`` element appearing at grid column *idx*. Raises |ValueError| if no ``w:tc`` element begins at that grid column. ...
Return a new `w:tbl` element having *rows* rows and *cols* columns with *width* distributed evenly between the columns. def new_tbl(cls, rows, cols, width): """ Return a new `w:tbl` element having *rows* rows and *cols* columns with *width* distributed evenly between the columns. ...
Set the value of `w:tblPr/w:tblStyle/@w:val` (a table style id) to *styleId*. If *styleId* is None, remove the `w:tblStyle` element. def tblStyle_val(self, styleId): """ Set the value of `w:tblPr/w:tblStyle/@w:val` (a table style id) to *styleId*. If *styleId* is None, remove the `w:tbl...
Return |False| if there is a ``<w:tblLayout>`` child with ``w:type`` attribute set to ``'fixed'``. Otherwise return |True|. def autofit(self): """ Return |False| if there is a ``<w:tblLayout>`` child with ``w:type`` attribute set to ``'fixed'``. Otherwise return |True|. """ ...
The row index that marks the bottom extent of the vertical span of this cell. This is one greater than the index of the bottom-most row of the span, similar to how a slice of the cell's rows would be specified. def bottom(self): """ The row index that marks the bottom extent of ...
Remove all content child elements, preserving the ``<w:tcPr>`` element if present. Note that this leaves the ``<w:tc>`` element in an invalid state because it doesn't contain at least one block-level element. It's up to the caller to add a ``<w:p>``child element as the last content eleme...
Generate a reference to each of the block-level content elements in this cell, in the order they appear. def iter_block_items(self): """ Generate a reference to each of the block-level content elements in this cell, in the order they appear. """ block_item_tags = (qn('w:...
Return the top-left ``<w:tc>`` element of a new span formed by merging the rectangular region defined by using this tc element and *other_tc* as diagonal corners. def merge(self, other_tc): """ Return the top-left ``<w:tc>`` element of a new span formed by merging the rectangula...