python-docx-oss 0.1.1__py3-none-any.whl → 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
docx/__init__.py CHANGED
@@ -13,7 +13,7 @@ from docx.api import Document
13
13
  if TYPE_CHECKING:
14
14
  from docx.opc.part import Part
15
15
 
16
- __version__ = "0.1.1"
16
+ __version__ = "0.2.0"
17
17
 
18
18
 
19
19
  __all__ = ["Document"]
docx/api.py CHANGED
@@ -5,6 +5,7 @@ Provides a syntactically more convenient API for interacting with the OpcPackage
5
5
 
6
6
  from __future__ import annotations
7
7
 
8
+ import io
8
9
  import os
9
10
  from typing import IO, TYPE_CHECKING, cast
10
11
 
@@ -16,18 +17,20 @@ if TYPE_CHECKING:
16
17
  from docx.parts.document import DocumentPart
17
18
 
18
19
 
19
- def Document(docx: str | IO[bytes] | None = None) -> DocumentObject:
20
+ def Document(docx: str | bytes | IO[bytes] | None = None) -> DocumentObject:
20
21
  """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
+ to a ``.docx`` file, raw DOCX bytes, or a binary file-like object.
22
23
 
23
24
  If `docx` is missing or ``None``, the built-in default document "template" is
24
25
  loaded.
25
26
  """
26
27
  docx = _default_docx_path() if docx is None else docx
28
+ source_name = "<bytes>" if isinstance(docx, bytes) else docx
29
+ docx = io.BytesIO(docx) if isinstance(docx, bytes) else docx
27
30
  document_part = cast("DocumentPart", Package.open(docx).main_document_part)
28
31
  if document_part.content_type != CT.WML_DOCUMENT_MAIN:
29
32
  tmpl = "file '%s' is not a Word file, content type is '%s'"
30
- raise ValueError(tmpl % (docx, document_part.content_type))
33
+ raise ValueError(tmpl % (source_name, document_part.content_type))
31
34
  return document_part.document
32
35
 
33
36
 
docx/document.py CHANGED
@@ -5,8 +5,10 @@
5
5
 
6
6
  from __future__ import annotations
7
7
 
8
+ import io
8
9
  from typing import IO, TYPE_CHECKING, Iterator, List, Sequence
9
10
 
11
+ from docx.api import Document as open_document
10
12
  from docx.blkcntnr import BlockItemContainer
11
13
  from docx.enum.section import WD_SECTION
12
14
  from docx.enum.text import WD_BREAK
@@ -182,6 +184,10 @@ class Document(ElementProxy):
182
184
  """A |CoreProperties| object providing Dublin Core properties of document."""
183
185
  return self._part.core_properties
184
186
 
187
+ def copy(self) -> Document:
188
+ """Return an independent in-memory snapshot of this document."""
189
+ return open_document(io.BytesIO(self.to_bytes()))
190
+
185
191
  @property
186
192
  def custom_properties(self) -> CustomProperties:
187
193
  """A |CustomProperties| object providing custom properties of document."""
@@ -249,6 +255,12 @@ class Document(ElementProxy):
249
255
  """
250
256
  return self._body.tables
251
257
 
258
+ def to_bytes(self) -> bytes:
259
+ """Serialize this document as a complete in-memory DOCX package."""
260
+ with io.BytesIO() as stream:
261
+ self.save(stream)
262
+ return stream.getvalue()
263
+
252
264
  @property
253
265
  def _block_width(self) -> Length:
254
266
  """A |Length| object specifying the space between margins in last section."""
docx/image/constants.py CHANGED
@@ -101,6 +101,7 @@ class MIME_TYPE:
101
101
  """Image content types."""
102
102
 
103
103
  BMP = "image/bmp"
104
+ EMF = "image/x-emf"
104
105
  GIF = "image/gif"
105
106
  JPEG = "image/jpeg"
106
107
  PNG = "image/png"
docx/image/emf.py CHANGED
@@ -13,7 +13,7 @@ class Emf(BaseImageHeader):
13
13
  @property
14
14
  def content_type(self):
15
15
  """
16
- MIME content type for this image, unconditionally `image/emf` for
16
+ MIME content type for this image, unconditionally `image/x-emf` for
17
17
  EMF images.
18
18
  """
19
19
  return MIME_TYPE.EMF
@@ -38,7 +38,6 @@ class Emf(BaseImageHeader):
38
38
  y1 = stream_rdr.read_long(20)
39
39
 
40
40
  frame = tuple(map(stream_rdr.read_long, (24, 28, 32, 36)))
41
- size = x1 - x0, y1 - y0
42
41
 
43
42
  xdpi = 2540.0 * (x1 - x0) / (frame[2] - frame[0])
44
43
  ydpi = 2540.0 * (y1 - y0) / (frame[3] - frame[1])
docx/opc/rel.py CHANGED
@@ -28,6 +28,11 @@ class Relationships(Dict[str, "_Relationship"]):
28
28
  self._target_parts_by_rId[rId] = target
29
29
  return rel
30
30
 
31
+ def __delitem__(self, rId: str) -> None:
32
+ """Remove the relationship and its cached target-part reference."""
33
+ super().__delitem__(rId)
34
+ self._target_parts_by_rId.pop(rId, None)
35
+
31
36
  def get_or_add(self, reltype: str, target_part: Part) -> _Relationship:
32
37
  """Return relationship of `reltype` to `target_part`, newly added if not already
33
38
  present in collection."""
docx/parts/customxml.py CHANGED
@@ -59,7 +59,7 @@ class CustomXmlPart(XmlPart):
59
59
  """
60
60
  return self.element.getchildren()
61
61
 
62
- def add_item(self, tag: str, text: str = "", **attrib):
62
+ def add_item(self, tag: str, text: str = "", **attrib: str) -> None:
63
63
  """
64
64
  Add a child element to root.
65
65
  """
docx/parts/document.py CHANGED
@@ -54,6 +54,24 @@ class DocumentPart(StoryPart):
54
54
  rId = self.relate_to(header_part, RT.HEADER)
55
55
  return header_part, rId
56
56
 
57
+ def delete_custom_xml_part(self, part: CustomXmlPart) -> None:
58
+ """Delete a Custom XML part related to this document.
59
+
60
+ Related Custom XML property parts are detached with the part and omitted from
61
+ the package when it is next saved.
62
+ """
63
+ matching_rIds = [
64
+ rId
65
+ for rId, rel in self.rels.items()
66
+ if rel.reltype == RT.CUSTOM_XML and not rel.is_external and rel.target_part is part
67
+ ]
68
+ if not matching_rIds:
69
+ raise ValueError(f"part '{part.partname}' is not related to this document")
70
+
71
+ part.drop_rels(lambda rel: rel.reltype == RT.CUSTOM_XML_PROPS)
72
+ for rId in matching_rIds:
73
+ del self.rels[rId]
74
+
57
75
  @property
58
76
  def comments(self) -> Comments:
59
77
  """|Comments| object providing access to the comments added to this document."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-docx-oss
3
- Version: 0.1.1
3
+ Version: 0.2.0
4
4
  Summary: Create, read, and update Microsoft Word .docx files.
5
5
  Author: Ethan St Lee
6
6
  License-Expression: MIT
@@ -53,18 +53,19 @@ Use it when an application needs more than basic paragraphs and tables: custom d
53
53
  metadata, application-owned XML, vector images, floating pictures, East Asian fonts,
54
54
  or low-level package and relationship access.
55
55
 
56
- ## Features
56
+ ## Capability Matrix
57
57
 
58
- | Capability | Support |
59
- | --- | --- |
60
- | Documents, paragraphs, runs, tables, styles, sections, headers, and footers | ✓ |
61
- | Custom document properties | ✓ |
62
- | Custom XML parts | ✓ |
63
- | SVG and EMF images | ✓ |
64
- | Inline and floating pictures | ✓ |
65
- | Independent ASCII, High ANSI, and East Asian font controls | ✓ |
66
- | Low-level OPC parts and relationships | ✓ |
67
- | File-path and in-memory stream processing | ✓ |
58
+ | Capability | Status | Primary API |
59
+ | --- | --- | --- |
60
+ | Documents, paragraphs, tables, styles, sections, headers, and footers | Stable | `Document()` and document object APIs |
61
+ | Custom document properties | Stable | `document.custom_properties` |
62
+ | Custom XML lifecycle | Stable | `add_custom_xml_part()`, `delete_custom_xml_part()` |
63
+ | SVG and EMF images | Stable | `add_picture()` |
64
+ | Inline and floating pictures | Stable | `add_picture()`, `add_float_picture()` |
65
+ | Latin and East Asian font controls | Stable | `run.font.name`, `run.font.hAnsi`, `run.font.eastAsia` |
66
+ | Low-level OPC parts and relationships | Stable | `document.part.package`, `part.rels` |
67
+ | In-memory read, export, and independent copies | Stable | `Document()`, `to_bytes()`, `copy()` |
68
+ | Package inspection, diff, and sanitization | Planned | — |
68
69
 
69
70
  ## Installation
70
71
 
@@ -170,6 +171,8 @@ available through `Document.add_float_picture()` and `Run.add_float_picture()`.
170
171
  - [API reference](docs/index.rst#api-documentation)
171
172
  - [Custom properties](docs/user/custom-properties.rst)
172
173
  - [Custom XML](docs/user/custom-xml.rst)
174
+ - [python-docx compatibility](docs/user/compatibility.rst)
175
+ - [Upstream maintenance](docs/dev/upstream-maintenance.rst)
173
176
  - [Release history](HISTORY.md)
174
177
 
175
178
  ## Quality and Testing
@@ -1,8 +1,8 @@
1
- docx/__init__.py,sha256=lzMxui-aFJFpJ33Mg8ra9T6A4KSd24ZH5sEHgXwgmcU,2102
2
- docx/api.py,sha256=13U56iPSM2tMRDEb03OmSZQkDAPmwGjfxosYkWS2tcc,1350
1
+ docx/__init__.py,sha256=aw9boQn8MrzqJFVCGmYy41YRdhhw3Ym0bIhqE643gCA,2102
2
+ docx/api.py,sha256=jIojXW4IRDEXQqaqvmOZbzBvNeyRXv9ntHByxd-Kb0M,1518
3
3
  docx/blkcntnr.py,sha256=_xe7Uz3RBCz4TPKXpIzRxtGg-1YPb3gAG9YvsSUyoLU,3525
4
4
  docx/comments.py,sha256=MmaP7U9mZOUFsRhaZNIbXatqOOA8-pOC6UlqWAuTHsc,6385
5
- docx/document.py,sha256=MbIMThSh8tEVExx72aKI4SMGQBcsScQdGLzNv2Asa9o,11941
5
+ docx/document.py,sha256=s9oolRg9W37W_8RG8v-rHVZtU9iBevV-NgnJ9JW2z00,12376
6
6
  docx/exceptions.py,sha256=WWiMy3ZtsfKRS31Wv09egLZnpPkX0H66B8JkOPCC3FM,454
7
7
  docx/package.py,sha256=Eob2q7R6jXQ3iPD0cAGHzu2RVYgU2te4zfG3oVN_-lM,4288
8
8
  docx/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -25,8 +25,8 @@ docx/enum/table.py,sha256=7PuQ--PGnSaNiE0Bu4VJ9t6LOgM-hNNmKEcl-3S2eUk,3943
25
25
  docx/enum/text.py,sha256=o6bdpJot2iY40UnnBQ1uGfdE8oHFm3dtXUrmt9eMlvE,10276
26
26
  docx/image/__init__.py,sha256=OvnRg5_DTmmrJObUBWLVcPFMRPb-u63e0EKfF_eIQlk,924
27
27
  docx/image/bmp.py,sha256=w_Jhu6hByAckJNyKhI3zHOKsHyibHgxDIjiPI9ev704,1347
28
- docx/image/constants.py,sha256=Y-ME9Z2KDLsjFQDun8XxhXrL6HNXhkUZqGHuCHXEMf4,3466
29
- docx/image/emf.py,sha256=BnPe6n9f4H9GZvB3Rb8zY-aaHr61WFLcQkryAqx4V4c,1210
28
+ docx/image/constants.py,sha256=c6Mtsy38b0nilCcRp9vvXkV7hyLDb0sDHfJoPqH-lPk,3490
29
+ docx/image/emf.py,sha256=C22sBpDu6vxLqAQmycPCb6xBc8ezVpr1C8x-C8SL2GE,1180
30
30
  docx/image/exceptions.py,sha256=_6rkjXXcbFxb_H-J0kK4Q1J9RaLx3TVQ5hoOVENvQaI,383
31
31
  docx/image/gif.py,sha256=0R7L0r7kjf844pdpN6UKEISsEQzOPAyA3cghXXdJW2s,1118
32
32
  docx/image/helpers.py,sha256=jy0fQBqIfstp9ExfKw3dGNKozn74RnmSA_YwmolsDqM,3089
@@ -47,7 +47,7 @@ docx/opc/part.py,sha256=15TjdwOs137LDdr-EVNeLb4iQdRyIfDZg6Vjx6_8Zdc,9606
47
47
  docx/opc/phys_pkg.py,sha256=-eNZDBbLShHo3W1iglf1CwN_nkK8qnmTWp6ABg77cjA,4005
48
48
  docx/opc/pkgreader.py,sha256=iUPZ7Ba4kNXjPB-q1mQEO44eVmbnP08po4WFH-HD2IE,9588
49
49
  docx/opc/pkgwriter.py,sha256=shDeuBtW2BbFOTHrXx8R97PWFaH_fE37JYBfbw6QWRs,4524
50
- docx/opc/rel.py,sha256=9BAZ59Cr5-BKs_5R8aifPDnPpXw-75GdfYYY6_a8Ays,6219
50
+ docx/opc/rel.py,sha256=ziqjJ_zQZ1Ow1sNM46se8F6YZRVST-Z8Z4NYvnnOt8o,6423
51
51
  docx/opc/shared.py,sha256=Zn2avg2g0J2fkG5YgFfCbl138gXex8stR-QwVJcuSIY,996
52
52
  docx/opc/spec.py,sha256=GP8YEb7bSmZQ0i338M3lQCVZm6yUnkUhm3PFKVgW8bQ,638
53
53
  docx/opc/parts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -81,8 +81,8 @@ docx/oxml/text/parfmt.py,sha256=5TYCf-QfcwlECXUkrLSYFfewFA1JieKGTtm3X01fu64,1222
81
81
  docx/oxml/text/run.py,sha256=W8vwOomvk1oGzAq2BqDgyVftKsSi5LK9_vlADVcMgpg,11238
82
82
  docx/parts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
83
83
  docx/parts/comments.py,sha256=Dmxh_i8ADRo-wLfO9LV0Lyho5XC_76oU1NJN8itw8lc,1760
84
- docx/parts/customxml.py,sha256=7GNGmzJf6-jBZ7ltciXp_3nfGDJGKI7y5jDCarbjVuI,1907
85
- docx/parts/document.py,sha256=lQSAco_MB0Y5hv7zDQGypR9JcYNi6HcF5Qsfn5c3ahg,7450
84
+ docx/parts/customxml.py,sha256=hbwDmPwR-1RRLOOFhmiPd0B-dNwpfZFrOxIaGU4UlG4,1920
85
+ docx/parts/document.py,sha256=hox7yRnsZRTF1C4ersAQUHNP58YhHxHMJnnztyp-2wM,8171
86
86
  docx/parts/hdrftr.py,sha256=_U0PCjVkK6EWwkyZb9oTunlNb6x55rECD1xFDzwUlPM,1709
87
87
  docx/parts/image.py,sha256=c-DF5YFvp1qVTx7Zon-xOXw0qPc_3LoeX9qMSFPs6mo,2739
88
88
  docx/parts/numbering.py,sha256=rrVIiRoBQIy6j8eWGhWyhcJvNx6RRaItWVDhorwrVyo,1715
@@ -125,8 +125,8 @@ docx/text/paragraph.py,sha256=N3F62_JRQZYaumuoaJXKCDnRSw2DIiOwzyMPV2Jd7GI,6828
125
125
  docx/text/parfmt.py,sha256=70M75HFo7ydpqmIdZjkV711muyrsBGdr4WZscF3YVJY,10339
126
126
  docx/text/run.py,sha256=tsW8dXOE4VnqEM7cJz_uU2Vq3qgdnrMGzyDq9x5jvE4,10800
127
127
  docx/text/tabstops.py,sha256=bJN98GWW22f4Sqg0dQbyUcnuOn65fHsNZuCGswI03_c,3896
128
- python_docx_oss-0.1.1.dist-info/licenses/LICENSE,sha256=lf2yZv-riPwFeah3IMDeG0zqI6fXcV9T9YOToeG54NM,1164
129
- python_docx_oss-0.1.1.dist-info/METADATA,sha256=vsUjUJmj42pvIO-LKhVnsuAxjpmU415JVPKJzSRfR4Q,7319
130
- python_docx_oss-0.1.1.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
131
- python_docx_oss-0.1.1.dist-info/top_level.txt,sha256=zkbrD3gmyqHpD_XhHe7lCbr2M5vT229nJ4UFqF5VjIs,5
132
- python_docx_oss-0.1.1.dist-info/RECORD,,
128
+ python_docx_oss-0.2.0.dist-info/licenses/LICENSE,sha256=lf2yZv-riPwFeah3IMDeG0zqI6fXcV9T9YOToeG54NM,1164
129
+ python_docx_oss-0.2.0.dist-info/METADATA,sha256=xgmccioh4zptF-ExSr52_7IrSCIsMQXeiovBXzT7DRk,7851
130
+ python_docx_oss-0.2.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
131
+ python_docx_oss-0.2.0.dist-info/top_level.txt,sha256=zkbrD3gmyqHpD_XhHe7lCbr2M5vT229nJ4UFqF5VjIs,5
132
+ python_docx_oss-0.2.0.dist-info/RECORD,,