@@ -16,9 +16,9 @@ First thing you'll need is a document to work on. The easiest way is this::
1616 document = Document()
1717
1818This 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
2424Adding a paragraph
@@ -29,13 +29,23 @@ headings and list items like bullets.
2929
3030Here'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
4151Adding a heading
@@ -218,8 +228,8 @@ This particular style causes the paragraph to appear as a bullet, a very handy
218228thing. You can also apply a style afterward. These two lines are equivalent to
219229the 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
224234The style is specified using its style ID, 'ListBullet' in this example.
225235Generally, 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,
248258it gets put into a single run. You can add more using the ``.add_run() `` method
249259on 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
254264This produces a paragraph that looks just like one created from a single
255265string. 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
262272set 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
269279which produces text that looks like this: 'Lorem ipsum **dolor ** sit amet.'
270280
271281Note that you can set bold or italic right on the result of ``.add_run() `` if
272282you 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
284294It's not necessary to provide text to the ``.add_paragraph() `` method. This can
285295make 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
293303Applying a character style
@@ -302,15 +312,15 @@ Like paragraph styles, a character style must already be defined in the document
302312
303313A 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
308318You can also apply a style to a run after it is created. This code produces
309319the 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
315325As with a paragraph style, the style ID is formed by removing the spaces in
316326the name as it appears in the Word UI. So the style 'Subtle Emphasis' would
0 commit comments