desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'The value of `w:sz/@w:val` or |None| if not present.'
@property def sz_val(self):
sz = self.sz if (sz is None): return None return sz.val
'Value of `w:u/@val`, or None if not present.'
@property def u_val(self):
u = self.u if (u is None): return None return u.val
'Return the value of the boolean child element having *name*, e.g. \'b\', \'i\', and \'smallCaps\'.'
def _get_bool_val(self, name):
element = getattr(self, name) if (element is None): return None return element.val
'The underline type corresponding to the ``w:val`` attribute value.'
@property def val(self):
val = self.get(qn('w:val')) underline = WD_UNDERLINE.from_xml(val) if (underline == WD_UNDERLINE.SINGLE): return True if (underline == WD_UNDERLINE.NONE): return False return underline
'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.'
@property def first_line_indent(self):
ind = self.ind if (ind is None): return None hanging = ind.hanging if (hanging is not None): return Length((- hanging)) firstLine = ind.firstLine if (firstLine is None): return None return firstLine
'The value of `w:ind/@w:left` or |None| if not present.'
@property def ind_left(self):
ind = self.ind if (ind is None): return None return ind.left
'The value of `w:ind/@w:right` or |None| if not present.'
@property def ind_right(self):
ind = self.ind if (ind is None): return None return ind.right
'The value of the ``<w:jc>`` child element or |None| if not present.'
@property def jc_val(self):
jc = self.jc if (jc is None): return None return jc.val
'The value of `keepLines/@val` or |None| if not present.'
@property def keepLines_val(self):
keepLines = self.keepLines if (keepLines is None): return None return keepLines.val
'The value of `keepNext/@val` or |None| if not present.'
@property def keepNext_val(self):
keepNext = self.keepNext if (keepNext is None): return None return keepNext.val
'The value of `pageBreakBefore/@val` or |None| if not present.'
@property def pageBreakBefore_val(self):
pageBreakBefore = self.pageBreakBefore if (pageBreakBefore is None): return None return pageBreakBefore.val
'The value of `w:spacing/@w:after` or |None| if not present.'
@property def spacing_after(self):
spacing = self.spacing if (spacing is None): return None return spacing.after
'The value of `w:spacing/@w:before` or |None| if not present.'
@property def spacing_before(self):
spacing = self.spacing if (spacing is None): return None return spacing.before
'The value of `w:spacing/@w:line` or |None| if not present.'
@property def spacing_line(self):
spacing = self.spacing if (spacing is None): return None return spacing.line
'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:line` if that behavior is desired.'
@property def spacing_lineRule(self):
spacing = self.spacing if (spacing is None): return None lineRule = spacing.lineRule if ((lineRule is None) and (spacing.line is not None)): return WD_LINE_SPACING.MULTIPLE return lineRule
'String contained in <w:pStyle> child, or None if that element is not present.'
@property def style(self):
pStyle = self.pStyle if (pStyle is None): return None return pStyle.val
'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.'
@style.setter def style(self, style):
if (style is None): self._remove_pStyle() return pStyle = self.get_or_add_pStyle() pStyle.val = style
'The value of `widowControl/@val` or |None| if not present.'
@property def widowControl_val(self):
widowControl = self.widowControl if (widowControl is None): return None return widowControl.val
'Insert a newly created `w:tab` child element in *pos* order.'
def insert_tab_in_order(self, pos, align, leader):
new_tab = self._new_tab() (new_tab.pos, new_tab.val, new_tab.leader) = (pos, align, leader) for tab in self.tab_lst: if (new_tab.pos < tab.pos): tab.addprevious(new_tab) return new_tab self.append(new_tab) return new_tab
'Return a newly added ``<w:t>`` element containing *text*.'
def add_t(self, 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):
drawing = self._add_drawing() drawing.append(inline_or_anchor) return drawing
'Remove all child elements except the ``<w:rPr>`` element if present.'
def clear_content(self):
content_child_elms = (self[1:] if (self.rPr is not None) else self[:]) for child in content_child_elms: self.remove(child)
'String contained in w:val attribute of <w:rStyle> grandchild, or |None| if that element is not present.'
@property def style(self):
rPr = self.rPr if (rPr is None): return None return rPr.style
'Set the character style of this <w:r> element to *style*. If *style* is None, remove the style element.'
@style.setter def style(self, style):
rPr = self.get_or_add_rPr() rPr.style = style
'A string representing the textual content of this run, with content child elements like ``<w:tab/>`` translated to their Python equivalent.'
@property def text(self):
text = '' for child in self: if (child.tag == qn('w:t')): t_text = child.text text += (t_text if (t_text is not None) else '') elif (child.tag == qn('w:tab')): text += ' DCTB ' elif (child.tag in (qn('w:br'), qn('w:cr'))): text += '\n' return text
'Create a "one-shot" ``_RunContentAppender`` instance and use it to append the run content elements corresponding to *text* to the ``<w:r>`` element *r*.'
@classmethod def append_to_run_from_text(cls, r, text):
appender = cls(r) appender.add_text(text)
'Append the run content elements corresponding to *text* to the ``<w:r>`` element of this instance.'
def add_text(self, text):
for char in text: self.add_char(char) self.flush()
'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 written.'
def add_char(self, char):
if (char == ' DCTB '): self.flush() self._r.add_tab() elif (char in '\r\n'): self.flush() self._r.add_br() else: self._bfr.append(char)
'Keep alpha hex numerals all uppercase just for consistency.'
@classmethod def convert_to_xml(cls, value):
return (u'%02X%02X%02X' % value)
'Return a list containing a reference to each ``<w:sectPr>`` element in the document, in the order encountered.'
@property def sectPr_lst(self):
return self.xpath('.//w:sectPr')
'Return the current ``<w:sectPr>`` element after adding a clone of it in a new ``<w:p>`` element appended to the block content elements. Note that the "current" ``<w:sectPr>`` will always be the sentinel sectPr in this case since we\'re always working at the end of the block content.'
def add_section_break(self):
sentinel_sectPr = self.get_or_add_sectPr() cloned_sectPr = sentinel_sectPr.clone() p = self.add_p() p.set_sectPr(cloned_sectPr) return sentinel_sectPr
'Remove all content child elements from this <w:body> element. Leave the <w:sectPr> element if it is present.'
def clear_content(self):
if (self.sectPr is not None): content_elms = self[:(-1)] else: content_elms = self[:] for content_elm in content_elms: self.remove(content_elm)
'Return the local part of the tag as a string. E.g. \'foobar\' is returned for tag \'f:foobar\'.'
@property def local_part(self):
return self._local_part
'Return a dict having a single member, mapping the namespace prefix of this tag to it\'s namespace name (e.g. {\'f\': \'http://foo/bar\'}). This is handy for passing to xpath calls and other uses.'
@property def nsmap(self):
return {self._pfx: self._ns_uri}
'Return the string namespace prefix for the tag, e.g. \'f\' is returned for tag \'f:foobar\'.'
@property def nspfx(self):
return self._pfx
'Return the namespace URI for the tag, e.g. \'http://foo/bar\' would be returned for tag \'f:foobar\' if the \'f\' prefix maps to \'http://foo/bar\' in nsmap.'
@property def nsuri(self):
return self._ns_uri
'Return a new ``<wp:inline>`` element populated with the values passed as parameters.'
@classmethod def new(cls, cx, cy, shape_id, pic):
inline = parse_xml(cls._inline_xml()) inline.extent.cx = cx inline.extent.cy = cy inline.docPr.id = shape_id inline.docPr.name = ('Picture %d' % shape_id) inline.graphic.graphicData.uri = 'http://schemas.openxmlformats.org/drawingml/2006/picture' inline.graphic.graphicData._insert_pic(pic) return inline
'Return a new `wp:inline` element containing the `pic:pic` element specified by the argument values.'
@classmethod def new_pic_inline(cls, shape_id, rId, filename, cx, cy):
pic_id = 0 pic = CT_Picture.new(pic_id, filename, rId, cx, cy) inline = cls.new(cx, cy, shape_id, pic) inline.graphic.graphicData._insert_pic(pic) return inline
'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.'
@classmethod def new(cls, pic_id, filename, rId, cx, cy):
pic = parse_xml(cls._pic_xml()) pic.nvPicPr.cNvPr.id = pic_id pic.nvPicPr.cNvPr.name = filename pic.blipFill.blip.embed = rId pic.spPr.cx = cx pic.spPr.cy = cy return pic
'Shape width as an instance of Emu, or None if not present.'
@property def cx(self):
xfrm = self.xfrm if (xfrm is None): return None return xfrm.cx
'Shape height as an instance of Emu, or None if not present.'
@property def cy(self):
xfrm = self.xfrm if (xfrm is None): return None return xfrm.cy
'|Length| object representing the bottom margin for all pages in this section in English Metric Units.'
@property def bottom_margin(self):
return self._sectPr.bottom_margin
'|Length| object representing the distance from the bottom edge of the page to the bottom edge of the footer. |None| if no setting is present in the XML.'
@property def footer_distance(self):
return self._sectPr.footer
'|Length| object representing the page gutter size in English Metric Units for all pages in this section. The page gutter is extra spacing added to the *inner* margin to ensure even margins after page binding.'
@property def gutter(self):
return self._sectPr.gutter
'|Length| object representing the distance from the top edge of the page to the top edge of the header. |None| if no setting is present in the XML.'
@property def header_distance(self):
return self._sectPr.header
'|Length| object representing the left margin for all pages in this section in English Metric Units.'
@property def left_margin(self):
return self._sectPr.left_margin
'Member of the :ref:`WdOrientation` enumeration specifying the page orientation for this section, one of ``WD_ORIENT.PORTRAIT`` or ``WD_ORIENT.LANDSCAPE``.'
@property def orientation(self):
return self._sectPr.orientation
'Total page height used for this section, inclusive of all edge spacing values such as margins. Page orientation is taken into account, so for example, its expected value would be ``Inches(8.5)`` for letter-sized paper when orientation is landscape.'
@property def page_height(self):
return self._sectPr.page_height
'Total page width used for this section, inclusive of all edge spacing values such as margins. Page orientation is taken into account, so for example, its expected value would be ``Inches(11)`` for letter-sized paper when orientation is landscape.'
@property def page_width(self):
return self._sectPr.page_width
'|Length| object representing the right margin for all pages in this section in English Metric Units.'
@property def right_margin(self):
return self._sectPr.right_margin
'The member of the :ref:`WdSectionStart` enumeration corresponding to the initial break behavior of this section, e.g. ``WD_SECTION.ODD_PAGE`` if the section should begin on the next odd page.'
@property def start_type(self):
return self._sectPr.start_type
'|Length| object representing the top margin for all pages in this section in English Metric Units.'
@property def top_margin(self):
return self._sectPr.top_margin
'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 color when one is assigned. In that case, the RGB value should be interpreted as no more than a good guess however, as the theme color takes precedence at rendering time. Its value is |None| whenever :attr:`type` is either |None| or `MSO_COLOR_TYPE.AUTO`. Assigning an |RGBColor| value causes :attr:`type` to become `MSO_COLOR_TYPE.RGB` and any theme color is removed. Assigning |None| causes any color to be removed such that the effective color is inherited from the style hierarchy.'
@property def rgb(self):
color = self._color if (color is None): return None if (color.val == ST_HexColorAuto.AUTO): return None return color.val
'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|. Assigning a member of :ref:`MsoThemeColorIndex` causes :attr:`type` to become `MSO_COLOR_TYPE.THEME`. Any existing RGB value is retained but ignored by Word. Assigning |None| causes any color specification to be removed such that the effective color is inherited from the style hierarchy.'
@property def theme_color(self):
color = self._color if ((color is None) or (color.themeColor is None)): return None return color.themeColor
'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.'
@property def type(self):
color = self._color if (color is None): return None if (color.themeColor is not None): return MSO_COLOR_TYPE.THEME if (color.val == ST_HexColorAuto.AUTO): return MSO_COLOR_TYPE.AUTO return MSO_COLOR_TYPE.RGB
'Return `w:rPr/w:color` or |None| if not present. Helper to factor out repetitive element access.'
@property def _color(self):
rPr = self._element.rPr if (rPr is None): return None return rPr.color
'The equivalent length expressed in centimeters (float).'
@property def cm(self):
return (self / float(self._EMUS_PER_CM))
'The equivalent length expressed in English Metric Units (int).'
@property def emu(self):
return self
'The equivalent length expressed in inches (float).'
@property def inches(self):
return (self / float(self._EMUS_PER_INCH))
'The equivalent length expressed in millimeters (float).'
@property def mm(self):
return (self / float(self._EMUS_PER_MM))
'Floating point length in points'
@property def pt(self):
return (self / float(self._EMUS_PER_PT))
'The equivalent length expressed in twips (int).'
@property def twips(self):
return int(round((self / float(self._EMUS_PER_TWIP))))
'Return a hex string rgb value, like \'3C2F80\''
def __str__(self):
return (u'%02X%02X%02X' % self)
'Return a new instance from an RGB color hex string like ``\'3C2F80\'``.'
@classmethod def from_string(cls, rgb_hex_str):
r = int(rgb_hex_str[:2], 16) g = int(rgb_hex_str[2:4], 16) b = int(rgb_hex_str[4:], 16) return cls(r, g, b)
'Return |True| if this proxy object refers to the same oxml element as does *other*. ElementProxy objects are value objects and should maintain no mutable local state. Equality for proxy objects is defined as referring to the same XML element, whether or not they are the same proxy object instance.'
def __eq__(self, other):
if (not isinstance(other, ElementProxy)): return False return (self._element is other._element)
'The lxml element proxied by this object.'
@property def element(self):
return self._element
'The package part containing this object'
@property def part(self):
return self._parent.part
'The package part containing this object'
@property def part(self):
return self._parent.part
'Called by loading code after all parts and relationships have been loaded, to afford the opportunity for any required post-processing.'
def after_unmarshal(self):
self._gather_image_parts()
'Collection of all image parts in this package.'
@lazyproperty def image_parts(self):
return ImageParts()
'Load the image part collection with all the image parts in package.'
def _gather_image_parts(self):
for rel in self.iter_rels(): if rel.is_external: continue if (rel.reltype != RT.IMAGE): continue if (rel.target_part in self.image_parts): continue self.image_parts.append(rel.target_part)
'Return an |ImagePart| instance containing the image identified by *image_descriptor*, newly created if a matching one is not present in the collection.'
def get_or_add_image_part(self, image_descriptor):
image = Image.from_file(image_descriptor) matching_image_part = self._get_by_sha1(image.sha1) if (matching_image_part is not None): return matching_image_part return self._add_image_part(image)
'Return an |ImagePart| instance newly created from image and appended to the collection.'
def _add_image_part(self, image):
partname = self._next_image_partname(image.ext) image_part = ImagePart.from_image(image, partname) self.append(image_part) return image_part
'Return the image part in this collection having a SHA1 hash matching *sha1*, or |None| if not found.'
def _get_by_sha1(self, sha1):
for image_part in self._image_parts: if (image_part.sha1 == sha1): return image_part return None
'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):
def image_partname(n): return PackURI((u'/word/media/image%d.%s' % (n, ext))) used_numbers = [image_part.partname.idx for image_part in self] for n in range(1, (len(self) + 1)): if (n not in used_numbers): return image_partname(n) return image_partname((len(self) + 1))
'MIME content type for this image, unconditionally `image/png` for PNG images.'
@property def content_type(self):
return MIME_TYPE.PNG
'Default filename extension, always \'png\' for PNG images.'
@property def default_ext(self):
return 'png'
'Return a |Png| instance having header properties parsed from image in *stream*.'
@classmethod def from_stream(cls, stream):
parser = _PngParser.parse(stream) px_width = parser.px_width px_height = parser.px_height horz_dpi = parser.horz_dpi vert_dpi = parser.vert_dpi return cls(px_width, px_height, horz_dpi, vert_dpi)
'Return a |_PngParser| instance containing the header properties parsed from the PNG image in *stream*.'
@classmethod def parse(cls, stream):
chunks = _Chunks.from_stream(stream) return cls(chunks)
'The number of pixels in each row of the image.'
@property def px_width(self):
IHDR = self._chunks.IHDR return IHDR.px_width
'The number of stacked rows of pixels in the image.'
@property def px_height(self):
IHDR = self._chunks.IHDR return IHDR.px_height
'Integer dots per inch for the width of this image. Defaults to 72 when not present in the file, as is often the case.'
@property def horz_dpi(self):
pHYs = self._chunks.pHYs if (pHYs is None): return 72 return self._dpi(pHYs.units_specifier, pHYs.horz_px_per_unit)
'Integer dots per inch for the height of this image. Defaults to 72 when not present in the file, as is often the case.'
@property def vert_dpi(self):
pHYs = self._chunks.pHYs if (pHYs is None): return 72 return self._dpi(pHYs.units_specifier, pHYs.vert_px_per_unit)
'Return dots per inch value calculated from *units_specifier* and *px_per_unit*.'
@staticmethod def _dpi(units_specifier, px_per_unit):
if ((units_specifier == 1) and px_per_unit): return int(round((px_per_unit * 0.0254))) return 72
'Return a |_Chunks| instance containing the PNG chunks in *stream*.'
@classmethod def from_stream(cls, stream):
chunk_parser = _ChunkParser.from_stream(stream) chunks = [chunk for chunk in chunk_parser.iter_chunks()] return cls(chunks)
'IHDR chunk in PNG image'
@property def IHDR(self):
match = (lambda chunk: (chunk.type_name == PNG_CHUNK_TYPE.IHDR)) IHDR = self._find_first(match) if (IHDR is None): raise InvalidImageStreamError('no IHDR chunk in PNG image') return IHDR
'pHYs chunk in PNG image, or |None| if not present'
@property def pHYs(self):
match = (lambda chunk: (chunk.type_name == PNG_CHUNK_TYPE.pHYs)) return self._find_first(match)
'Return first chunk in stream order returning True for function *match*.'
def _find_first(self, match):
for chunk in self._chunks: if match(chunk): return chunk return None
'Return a |_ChunkParser| instance that can extract the chunks from the PNG image in *stream*.'
@classmethod def from_stream(cls, stream):
stream_rdr = StreamReader(stream, BIG_ENDIAN) return cls(stream_rdr)
'Generate a |_Chunk| subclass instance for each chunk in this parser\'s PNG stream, in the order encountered in the stream.'
def iter_chunks(self):
for (chunk_type, offset) in self._iter_chunk_offsets(): chunk = _ChunkFactory(chunk_type, self._stream_rdr, offset) (yield chunk)
'Generate a (chunk_type, chunk_offset) 2-tuple for each of the chunks in the PNG image stream. Iteration stops after the IEND chunk is returned.'
def _iter_chunk_offsets(self):
chunk_offset = 8 while True: chunk_data_len = self._stream_rdr.read_long(chunk_offset) chunk_type = self._stream_rdr.read_str(4, chunk_offset, 4) data_offset = (chunk_offset + 8) (yield (chunk_type, data_offset)) if (chunk_type == 'IEND'): break chunk_offset += (((4 + 4) + chunk_data_len) + 4)
'Return a default _Chunk instance that only knows its chunk type.'
@classmethod def from_offset(cls, chunk_type, stream_rdr, offset):
return cls(chunk_type)
'The chunk type name, e.g. \'IHDR\', \'pHYs\', etc.'
@property def type_name(self):
return self._chunk_type
'Return an _IHDRChunk instance containing the image dimensions extracted from the IHDR chunk in *stream* at *offset*.'
@classmethod def from_offset(cls, chunk_type, stream_rdr, offset):
px_width = stream_rdr.read_long(offset) px_height = stream_rdr.read_long(offset, 4) return cls(chunk_type, px_width, px_height)
'Return a _pHYsChunk instance containing the image resolution extracted from the pHYs chunk in *stream* at *offset*.'
@classmethod def from_offset(cls, chunk_type, stream_rdr, offset):
horz_px_per_unit = stream_rdr.read_long(offset) vert_px_per_unit = stream_rdr.read_long(offset, 4) units_specifier = stream_rdr.read_byte(offset, 8) return cls(chunk_type, horz_px_per_unit, vert_px_per_unit, units_specifier)
'Return the MIME type of this TIFF image, unconditionally the string ``image/tiff``.'
@property def content_type(self):
return MIME_TYPE.TIFF
'Default filename extension, always \'tiff\' for TIFF images.'
@property def default_ext(self):
return 'tiff'
'Return a |Tiff| instance containing the properties of the TIFF image in *stream*.'
@classmethod def from_stream(cls, stream):
parser = _TiffParser.parse(stream) px_width = parser.px_width px_height = parser.px_height horz_dpi = parser.horz_dpi vert_dpi = parser.vert_dpi return cls(px_width, px_height, horz_dpi, vert_dpi)
'Return an instance of |_TiffParser| containing the properties parsed from the TIFF image in *stream*.'
@classmethod def parse(cls, stream):
stream_rdr = cls._make_stream_reader(stream) ifd0_offset = stream_rdr.read_long(4) ifd_entries = _IfdEntries.from_stream(stream_rdr, ifd0_offset) return cls(ifd_entries)
'The horizontal dots per inch value calculated from the XResolution and ResolutionUnit tags of the IFD; defaults to 72 if those tags are not present.'
@property def horz_dpi(self):
return self._dpi(TIFF_TAG.X_RESOLUTION)