Skip to content

Commit 759e7c4

Browse files
author
Steve Canny
committed
parts: add _Document.blob
1 parent 7505083 commit 759e7c4

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

docx/parts.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from opc import Part
1515

16-
from docx.oxml.base import oxml_fromstring
16+
from docx.oxml.base import oxml_fromstring, oxml_tostring
1717
from docx.text import Paragraph
1818

1919

@@ -25,6 +25,11 @@ def __init__(self, partname, content_type, document_elm):
2525
super(_Document, self).__init__(partname, content_type)
2626
self._element = document_elm
2727

28+
@property
29+
def blob(self):
30+
return oxml_tostring(self._element, encoding='UTF-8',
31+
pretty_print=False, standalone=True)
32+
2833
@property
2934
def body(self):
3035
"""

tests/test_parts.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def init(self, request):
3333
def oxml_fromstring_(self, request):
3434
return function_mock('docx.parts.oxml_fromstring', request)
3535

36+
@pytest.fixture
37+
def oxml_tostring(self, request):
38+
return function_mock('docx.parts.oxml_tostring', request)
39+
3640
def it_can_be_constructed_by_opc_part_factory(
3741
self, oxml_fromstring_, init):
3842
# mockery ----------------------
@@ -58,6 +62,17 @@ def it_has_a_body(self, init, _Body_):
5862
_Body_.assert_called_once_with(doc._element.body)
5963
assert body is _Body_.return_value
6064

65+
def it_can_serialize_to_xml(self, init, oxml_tostring):
66+
# mockery ----------------------
67+
doc = _Document(None, None, None)
68+
doc._element = Mock(name='_element')
69+
# exercise ---------------------
70+
doc.blob
71+
# verify -----------------------
72+
oxml_tostring.assert_called_once_with(
73+
doc._element, encoding='UTF-8', pretty_print=False,
74+
standalone=True)
75+
6176

6277
class Describe_Body(object):
6378

0 commit comments

Comments
 (0)