Skip to content

add_text() on Paragraph #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docx/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ def add_run(self, text=None, style=None):
run.style = style
return run

def add_text(self, text):
"""
Provides an easy way to add text to a paragraph, preserving the style
of the previously inserted text, If the paragraph has no runs, a new
run is created with the given text. Otherwise, the text is appended
to the last run and takes on its style.
"""
if self._p.r_lst:
self.runs[-1].add_text(text)
else:
self.add_run(text)

@property
def alignment(self):
"""
Expand Down
18 changes: 18 additions & 0 deletions features/par-add-text.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Feature: Add text to a paragraph, preserving the style of the last text
In order to easily add new text to the end of a paragraph
As a python-docx programmer
I want an easy way to add text to the last run

Scenario: Add text to an empty paragraph
Given a paragraph
Then the paragraph has no content
When I add text to the paragraph
Then the paragraph has the text I added

Scenario: Add text to a non-empty paragraph with a style on the last run
Given a paragraph with some text and a style on the last run
When I add text to the paragraph
Then the paragraph has the same amount of runs
And the paragraph ends with the text I added
And the initial text is still there
And the last run still has the same style
38 changes: 38 additions & 0 deletions features/steps/paragraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ def given_a_paragraph_with_content_and_formatting(context):
context.paragraph = Paragraph(p)


@given('a paragraph with some text and a style on the last run')
def given_a_paragraph_with_some_text_and_style_on_last_run(context):
context.document = Document()
context.paragraph = context.document.add_paragraph()
context.paragraph.text = 'Lorem ipsum '
context.paragraph.runs[-1].style = 'Heading1'


# when ====================================================

@when('I add a run to the paragraph')
Expand Down Expand Up @@ -86,6 +94,11 @@ def when_I_set_the_paragraph_text(context):
context.paragraph.text = 'bar\tfoo\r'


@when('I add text to the paragraph')
def when_I_add_text_to_the_paragraph(context):
context.paragraph.add_text('dolor sit amet')


# then =====================================================

@then('the document contains four paragraphs')
Expand Down Expand Up @@ -140,6 +153,31 @@ def then_the_style_of_the_second_paragraph_matches_the_style_I_set(context):
assert second_paragraph.style == 'Heading1'


@then('the paragraph has the same amount of runs')
def then_the_paragraph_has_same_runs(context):
assert len(context.paragraph.runs) == 1


@then('the paragraph ends with the text I added')
def then_the_paragraph_ends_with_the_text_I_added(context):
assert context.paragraph.text.endswith('dolor sit amet')


@then('the paragraph has the text I added')
def then_the_paragraph_has_the_text_I_added(context):
assert context.paragraph.text == 'dolor sit amet'


@then('the initial text is still there')
def then_the_initial_text_is_still_there(context):
assert context.paragraph.text.startswith('Lorem ipsum ')


@then('the last run still has the same style')
def then_last_run_still_has_same_style(context):
assert context.paragraph.runs[-1].style == 'Heading1'


@then('the text of the second paragraph matches the text I set')
def then_the_text_of_the_second_paragraph_matches_the_text_I_set(context):
second_paragraph = context.document.paragraphs[1]
Expand Down