Пример #1
0
def Document(docx=None, word_open_xml=None):
    """
    Return a |Document| object loaded from *docx*, where *docx* can be
    either a path to a ``.docx`` file (a string) or a file-like object.
    Optionally, ``word_open_xml`` can be specified as a string of xml.
    Either ``docx`` or `word_open_xml`` may be specified, but not both. If
    *docx* is missing or ``None`` and *open_word_xml* is None, the built-in
    default document "template" is loaded.
    """
    if docx and word_open_xml:
        msg = "Must either specify docx or word_open_xml, but not both"
        raise ValueError(msg)

    if word_open_xml is None:
        docx = _default_docx_path() if docx is None else docx
        document_part = Package.open(docx).main_document_part
        if document_part.content_type != CT.WML_DOCUMENT_MAIN:
            tmpl = "file '%s' is not a Word file, content type is '%s'"
            raise ValueError(tmpl % (docx, document_part.content_type))
    else:
        # word_open_xml
        document_part = Package.open(word_open_xml,
                                     is_from_file=False).main_document_part

        if document_part.content_type != CT.WML_DOCUMENT_MAIN:
            tmpl = "string '%s' is not a Word document, content type is '%s'"
            raise ValueError(tmpl %
                             (word_open_xml, document_part.content_type))

    return document_part.document
Пример #2
0
    def it_can_get_or_add_an_image_part_containing_a_specified_image(
            self, image_parts_prop_, image_parts_, image_part_):
        image_parts_prop_.return_value = image_parts_
        image_parts_.get_or_add_image_part.return_value = image_part_
        package = Package()

        image_part = package.get_or_add_image_part("image.png")

        image_parts_.get_or_add_image_part.assert_called_once_with("image.png")
        assert image_part is image_part_
Пример #3
0
 def __init__(self, docx, strict=False):
     """
     Initialize the DocxTemplate with the given docx file or stream.
     :param docx: A string or file like object (django.core.File objects work) representing a docx file.
     :param strict: will make the template render throw an error when fields and context do not match if True
     """
     self.strict = strict
     document_part = Package.open(docx).main_document_part
     if document_part.content_type != CONTENT_TYPE.WML_DOCUMENT_MAIN:
         tmpl = "file '%s' is not a Word file, content type is '%s'"
         raise ValueError(tmpl % (docx, document_part.content_type))
     super().__init__(document_part._element, document_part)
Пример #4
0
def Document(docx=None):
    """
    Return a |Document| object loaded from *docx*, where *docx* can be
    either a path to a ``.docx`` file (a string) or a file-like object. If
    *docx* is missing or ``None``, the built-in default document "template"
    is loaded.
    """
    docx = _default_docx_path() if docx is None else docx
    document_part = Package.open(docx).main_document_part
    if document_part.content_type != CT.WML_DOCUMENT_MAIN:
        tmpl = "file '%s' is not a Word file, content type is '%s'"
        raise ValueError(tmpl % (docx, document_part.content_type))
    return document_part.document
Пример #5
0
def Document(docx=None):
    """
    Return a |Document| object loaded from *docx*, where *docx* can be
    either a path to a ``.docx`` file (a string) or a file-like object. If
    *docx* is missing or ``None``, the built-in default document "template"
    is loaded.
    """
    docx = _default_docx_path() if docx is None else docx
    document_part = Package.open(docx).main_document_part
    if document_part.content_type != CT.WML_DOCUMENT_MAIN:
        tmpl = "file '%s' is not a Word file, content type is '%s'"
        raise ValueError(tmpl % (docx, document_part.content_type))
    return document_part.document
Пример #6
0
 def it_gathers_package_image_parts_after_unmarshalling(self):
     package = Package.open(docx_path('having-images'))
     image_parts = package.image_parts
     assert len(image_parts) == 3
     for image_part in image_parts:
         assert isinstance(image_part, ImagePart)
Пример #7
0
 def it_gathers_package_image_parts_after_unmarshalling(self):
     package = Package.open(docx_path('having-images'))
     image_parts = package.image_parts
     assert len(image_parts) == 3
     for image_part in image_parts:
         assert isinstance(image_part, ImagePart)
Пример #8
0
def Document(docx=None):
    """
    Return a |Document| object loaded from *docx*, where *docx* can be
    either a path to a ``.docx`` file (a string) or a file-like object. If
    *docx* is missing or ``None``, the built-in default document "template"
    is loaded.
    """
    docx = _default_docx_path() if docx is None else docx
    document_part = Package.open(docx).main_document_part
    if document_part.content_type != CT.WML_DOCUMENT_MAIN:
        tmpl = "file '%s' is not a Word file, content type is '%s'"
        raise ValueError(tmpl % (docx, document_part.content_type))
    return document_part.document
    def __init__(self, docx=None):
        super(Document, self).__init__()
        document_part, package = self._open(docx)
        self._document_part = document_part
        self._package = package

    def add_heading(self, text='', level=1):
        """
        Return a heading paragraph newly added to the end of the document,
        populated with *text* and having the heading paragraph style
        determined by *level*. If *level* is 0, the style is set to
        ``'Title'``. If *level* is 1 (or not present), ``'Heading1'`` is used.
        Otherwise the style is set to ``'Heading{level}'``. If *level* is
        outside the range 0-9, |ValueError| is raised.
        """
        if not 0 <= level <= 9:
            raise ValueError("level must be in range 0-9, got %d" % level)
        style = 'Title' if level == 0 else 'Heading%d' % level
        return self.add_paragraph(text, style)

    def add_page_break(self):
        """
        Return a paragraph newly added to the end of the document and
        containing only a page break.
        """
        p = self._document_part.add_paragraph()
        r = p.add_run()
        r.add_break(WD_BREAK.PAGE)
        return p

    def add_paragraph(self, text='', style=None):
        """
        Return a paragraph newly added to the end of the document, populated
        with *text* and having paragraph style *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``) characters, each of which is converted to a line
        break.
        """
        return self._document_part.add_paragraph(text, style)

    def add_list(self, style='ListParagraph', level=0):
        """
        Return a helper that implements methods to create a list formed by
        paragraphs sharing the same numId, added to the end of the document,
        having paragraph style *style* and indentation level *level*.
        """
        return self._document_part.add_list(style=style, level=level)

    def add_picture(self, image_path_or_stream, width=None, height=None):
        """
        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 is used to compute
        a scaling factor that is then applied to the unspecified dimension,
        preserving the aspect ratio of the image. The native size of the
        picture is calculated using the dots-per-inch (dpi) value specified
        in the image file, defaulting to 72 dpi if no value is specified, as
        is often the case.
        """
        run = self.add_paragraph().add_run()
        picture = run.add_picture(image_path_or_stream, width, height)
        return picture

    def add_section(self, start_type=WD_SECTION.NEW_PAGE):
        """
        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 defaulting to
        ``WD_SECTION.NEW_PAGE`` if not provided.
        """
        return self._document_part.add_section(start_type)

    def add_table(self, rows, cols, style='LightShading-Accent1'):
        """
        Add a table having row and column counts of *rows* and *cols*
        respectively and table style of *style*. If *style* is |None|, a
        table with no style is produced.
        """
        table = self._document_part.add_table(rows, cols)
        if style:
            table.style = style
        return table