Skip to content

Commit 1a05663

Browse files
author
Steve Canny
committed
docs: add user docs for insert_paragraph_before()
1 parent 65db853 commit 1a05663

File tree

3 files changed

+44
-33
lines changed

3 files changed

+44
-33
lines changed

docs/dev/analysis/features/run-content.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Possible strategy:
2323
2. Move ``<w:rPr>`` child to new r element, if there is one
2424
3. Delete initial ``<w:r>``
2525
4. Set self._r of Run to new r element
26-
4. Return self
26+
5. Return self
2727

2828

2929
Schema excerpt

docs/user/quickstart.rst

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ First thing you'll need is a document to work on. The easiest way is this::
1616
document = Document()
1717

1818
This opens up a blank document based on the default "template", pretty much
19-
what you get when you start a new document in Word using the built-in defaults.
20-
You can open any Word document using |docx|, but we'll keep things simple for
21-
the moment.
19+
what you get when you start a new document in Word using the built-in
20+
defaults. You can open and work on an existing Word document using |docx|,
21+
but we'll keep things simple for the moment.
2222

2323

2424
Adding a paragraph
@@ -29,13 +29,23 @@ headings and list items like bullets.
2929

3030
Here's the simplest way to add one::
3131

32-
p = document.add_paragraph('Lorem ipsum dolor sit amet.')
32+
paragraph = document.add_paragraph('Lorem ipsum dolor sit amet.')
3333

34-
This method returns a reference to the newly added paragraph, assigned to ``p``
35-
in this case. I'll be leaving that out in the following examples unless I have
36-
a need for it. In your code, often times you won't be doing anything with the
37-
item after you've added it, so there's not a lot of sense in keep a reference
38-
to it hanging around.
34+
This method returns a reference to a paragraph, newly added paragraph at the
35+
end of the document. The new paragraph reference is assigned to ``paragraph``
36+
in this case, but I'll be leaving that out in the following examples unless
37+
I have a need for it. In your code, often times you won't be doing anything
38+
with the item after you've added it, so there's not a lot of sense in keep
39+
a reference to it hanging around.
40+
41+
It's also possible to use one paragraph as a "cursor" and insert a new
42+
paragraph directly above it::
43+
44+
prior_paragraph = paragraph.insert_paragraph_before('Lorem ipsum')
45+
46+
This allows a paragraph to be inserted in the middle of a document, something
47+
that's often important when modifying an existing document rather than
48+
generating one from scratch.
3949

4050

4151
Adding a heading
@@ -218,8 +228,8 @@ This particular style causes the paragraph to appear as a bullet, a very handy
218228
thing. You can also apply a style afterward. These two lines are equivalent to
219229
the one above::
220230

221-
p = document.add_paragraph('Lorem ipsum dolor sit amet.')
222-
p.style = 'ListBullet'
231+
paragraph = document.add_paragraph('Lorem ipsum dolor sit amet.')
232+
paragraph.style = 'ListBullet'
223233

224234
The style is specified using its style ID, 'ListBullet' in this example.
225235
Generally, the style ID is formed by removing the spaces in the style name as
@@ -248,8 +258,8 @@ When you add a paragraph by providing text to the ``.add_paragraph()`` method,
248258
it gets put into a single run. You can add more using the ``.add_run()`` method
249259
on the paragraph::
250260

251-
p = document.add_paragraph('Lorem ipsum ')
252-
p.add_run('dolor sit amet.')
261+
paragraph = document.add_paragraph('Lorem ipsum ')
262+
paragraph.add_run('dolor sit amet.')
253263

254264
This produces a paragraph that looks just like one created from a single
255265
string. It's not apparent where paragraph text is broken into runs unless you
@@ -261,33 +271,33 @@ that one a few times :).
261271
|Run| objects have both a ``.bold`` and ``.italic`` property that allows you to
262272
set their value for a run::
263273

264-
p = document.add_paragraph('Lorem ipsum ')
265-
r = p.add_run('dolor')
266-
r.bold = True
267-
p.add_run(' sit amet.')
274+
paragraph = document.add_paragraph('Lorem ipsum ')
275+
run = paragraph.add_run('dolor')
276+
run.bold = True
277+
paragraph.add_run(' sit amet.')
268278

269279
which produces text that looks like this: 'Lorem ipsum **dolor** sit amet.'
270280

271281
Note that you can set bold or italic right on the result of ``.add_run()`` if
272282
you don't need it for anything else::
273283

274-
p.add_run('dolor').bold = True
284+
paragraph.add_run('dolor').bold = True
275285
276286
# is equivalent to:
277287

278-
r = p.add_run('dolor')
279-
r.bold = True
288+
run = paragraph.add_run('dolor')
289+
run.bold = True
280290

281-
# except you don't have a reference to `r` afterward
291+
# except you don't have a reference to `run` afterward
282292
283293

284294
It's not necessary to provide text to the ``.add_paragraph()`` method. This can
285295
make your code simpler if you're building the paragraph up from runs anyway::
286296

287-
p = document.add_paragraph()
288-
p.add_run('Lorem ipsum ')
289-
p.add_run('dolor').bold = True
290-
p.add_run(' sit amet.')
297+
paragraph = document.add_paragraph()
298+
paragraph.add_run('Lorem ipsum ')
299+
paragraph.add_run('dolor').bold = True
300+
paragraph.add_run(' sit amet.')
291301
292302

293303
Applying a character style
@@ -302,15 +312,15 @@ Like paragraph styles, a character style must already be defined in the document
302312

303313
A character style can be specified when adding a new run::
304314

305-
p = document.add_paragraph('Normal text, ')
306-
p.add_run('text with emphasis.', 'Emphasis')
315+
paragraph = document.add_paragraph('Normal text, ')
316+
paragraph.add_run('text with emphasis.', 'Emphasis')
307317

308318
You can also apply a style to a run after it is created. This code produces
309319
the same result as the lines above::
310320

311-
p = document.add_paragraph('Normal text, ')
312-
r = p.add_run('text with emphasis.')
313-
r.style = 'Emphasis'
321+
paragraph = document.add_paragraph('Normal text, ')
322+
run = paragraph.add_run('text with emphasis.')
323+
run.style = 'Emphasis'
314324

315325
As with a paragraph style, the style ID is formed by removing the spaces in
316326
the name as it appears in the Word UI. So the style 'Subtle Emphasis' would

docx/text.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ def add_run(self, text=None, style=None):
7474
def insert_paragraph_before(self, text=None, style=None):
7575
"""
7676
Return a newly created paragraph, inserted directly before this
77-
paragraph, and having its text set to *text* and style set to
78-
*style*.
77+
paragraph. If *text* is supplied, the new paragraph contains that
78+
text in a single run. If *style* is provided, that style is assigned
79+
to the new paragraph.
7980
"""
8081
p = self._p.add_p_before()
8182
paragraph = Paragraph(p)

0 commit comments

Comments
 (0)