Skip to content

Commit c254f8e

Browse files
author
Steve Canny
committed
accept: add add_paragraph.feature
1 parent 92ad0e1 commit c254f8e

5 files changed

Lines changed: 98 additions & 1 deletion

File tree

docx/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
# This module is part of python-docx and is released under the MIT License:
88
# http://www.opensource.org/licenses/mit-license.php
99

10+
from docx.api import Document # noqa
11+
1012
__version__ = '0.3.0d1'

docx/api.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# api.py
4+
#
5+
# Copyright (C) 2012, 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+
Directly exposed API functions and classes, :func:`Document` for now.
12+
Provides a syntactically more convenient API for interacting with the
13+
opc.OpcPackage graph.
14+
"""
15+
16+
17+
def Document(docx=None):
18+
"""
19+
Return a |_Document| instance loaded from *docx*, where *docx* can be
20+
either a path to a ``.docx`` file (a string) or a file-like object. If
21+
*docx* is missing or ``None``, the built-in default document "template"
22+
is loaded.
23+
"""

features/add_paragraph.feature

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Feature: Add a paragraph of text
2+
In order to add text to a document
3+
As an python-docx developer
4+
I need to add a paragraph
5+
6+
@wip
7+
Scenario: Add a paragraph to a document created from the default template
8+
Given a new document created from the default template
9+
When I add a new paragraph to the body
10+
And I add a new run to the paragraph
11+
And I add new text to the run
12+
And I save the document
13+
Then the document contains the text I added

features/dummy.feature

Lines changed: 0 additions & 1 deletion
This file was deleted.

features/steps/docx_steps.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,63 @@
88
# http://www.opensource.org/licenses/mit-license.php
99

1010
"""Step implementations for python-docx acceptance tests"""
11+
12+
import os
13+
14+
from behave import given, then, when
15+
16+
from docx import Document
17+
18+
19+
def absjoin(*paths):
20+
return os.path.abspath(os.path.join(*paths))
21+
22+
thisdir = os.path.split(__file__)[0]
23+
scratch_dir = absjoin(thisdir, '../_scratch')
24+
saved_docx_path = absjoin(scratch_dir, 'test_out.docx')
25+
26+
test_text = 'python-docx was here!'
27+
28+
29+
# given ===================================================
30+
31+
@given('a new document created from the default template')
32+
def step_given_new_doc_from_def_template(context):
33+
context.doc = Document()
34+
35+
36+
# when ====================================================
37+
38+
@when('I add a new paragraph to the body')
39+
def step_when_add_new_paragraph_to_body(context):
40+
body = context.doc.body
41+
context.p = body.add_paragraph()
42+
43+
44+
@when('I add a new run to the paragraph')
45+
def step_when_add_new_run_to_paragraph(context):
46+
context.r = context.p.add_run()
47+
48+
49+
@when('I add new text to the run')
50+
def step_when_add_new_text_to_run(context):
51+
context.r.add_text(test_text)
52+
53+
54+
@when('I save the document')
55+
def step_when_save_document(context):
56+
if os.path.isfile(saved_docx_path):
57+
os.remove(saved_docx_path)
58+
context.doc.save(saved_docx_path)
59+
60+
61+
# then =====================================================
62+
63+
@then('the document contains the text I added')
64+
def step_then_document_contains_text_I_added(context):
65+
doc = Document(saved_docx_path)
66+
body = doc.body
67+
paragraphs = body.paragraphs
68+
p = paragraphs[-1]
69+
r = p.runs[0]
70+
assert r.text == test_text

0 commit comments

Comments
 (0)