Skip to content

Commit 946ab5e

Browse files
author
Steve Canny
committed
api: add _Document.body
1 parent 0252990 commit 946ab5e

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

docx/api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ class _Document(object):
4646
"""
4747
def __init__(self, pkg, document_part):
4848
super(_Document, self).__init__()
49+
self._document = document_part
50+
51+
@property
52+
def body(self):
53+
"""
54+
Return a reference to the |Body| instance for this document.
55+
"""
56+
return self._document.body

docx/parts.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# parts.py
4+
#
5+
# Copyright (C) 2013 Steve Canny scanny@cisco.com
6+
#
7+
# This module is part of python-docx and is released under the MIT License:
8+
# http://www.opensource.org/licenses/mit-license.php
9+
10+
"""
11+
Document parts such as _Document, and closely related classes.
12+
"""
13+
14+
from opc import Part
15+
16+
17+
class _Document(Part):
18+
"""
19+
Main document part of a WordprocessingML (WML) package, aka a .docx file.
20+
"""
21+
@property
22+
def body(self):
23+
"""
24+
The |_Body| instance containing the content for this document.
25+
"""

tests/test_api.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
import pytest
1313

14-
from mock import Mock, PropertyMock
14+
from mock import create_autospec, Mock, PropertyMock
1515

16+
from docx import parts
1617
from docx.api import Document, _Document
1718
from opc.constants import CONTENT_TYPE as CT
1819

@@ -69,3 +70,14 @@ def it_should_raise_if_not_a_docx_file(self, OpcPackage_mockery):
6970
# verify -----------------------
7071
with pytest.raises(ValueError):
7172
Document(docx)
73+
74+
75+
class Describe_Document(object):
76+
77+
def it_provides_access_to_the_document_body(self):
78+
document_part = create_autospec(parts._Document)
79+
doc = _Document(None, document_part)
80+
# exercise ---------------------
81+
body = doc.body
82+
# verify -----------------------
83+
assert body is document_part.body

0 commit comments

Comments
 (0)