MCPcopy Index your code
hub / github.com/python-openxml/python-docx / Document

Function Document

src/docx/api.py:19–31  ·  view source on GitHub ↗

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: str | IO[bytes] | None = None)

Source from the content-addressed store, hash-verified

17
18
19def Document(docx: str | IO[bytes] | None = None) -> DocumentObject:
20 """Return a |Document| object loaded from `docx`, where `docx` can be either a path
21 to a ``.docx`` file (a string) or a file-like object.
22
23 If `docx` is missing or ``None``, the built-in default document "template" is
24 loaded.
25 """
26 docx = _default_docx_path() if docx is None else docx
27 document_part = cast("DocumentPart", Package.open(docx).main_document_part)
28 if document_part.content_type != CT.WML_DOCUMENT_MAIN:
29 tmpl = "file '%s' is not a Word file, content type is '%s'"
30 raise ValueError(tmpl % (docx, document_part.content_type))
31 return document_part.document
32
33
34def _default_docx_path():

Calls 2

_default_docx_pathFunction · 0.85
openMethod · 0.80

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…