Skip to content

Commit ad99e19

Browse files
author
Steve Canny
committed
docs: add shapes page to user guide
1 parent 42f9a4c commit ad99e19

File tree

4 files changed

+68
-34
lines changed

4 files changed

+68
-34
lines changed

docs/api/document.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ Document objects
1010
.. currentmodule:: docx.api
1111

1212

13-
|_Document| objects
14-
-------------------
13+
|Document| objects
14+
------------------
1515

16-
.. autoclass:: _Document
16+
17+
.. autoclass:: Document
1718
:members:

docs/index.rst

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,44 @@ A post-alpha release is expected within a few weeks, say Feb 1, 2014.
2121
What it can do
2222
--------------
2323

24+
.. |img| image:: /_static/img/example-docx-01.png
25+
2426
Here's an example of what |docx| can do:
2527

26-
.. figure:: /_static/img/example-docx-01.png
28+
====== ======================================================================
29+
|img| ::
30+
31+
from docx import Document
2732

28-
|
33+
document = Document()
2934

30-
::
35+
document.add_heading('Document Title', 0)
36+
document.add_paragraph('A plain paragraph.')
3137

32-
from docx import Document
38+
document.add_heading('Heading, level 1', level=1)
39+
document.add_paragraph('Intense quote', style='IntenseQuote')
3340

34-
document = Document()
41+
document.add_bullet('first item in unordered list')
42+
document.add_list_item('first item in ordered list')
3543

36-
document.add_heading('Document Title', 0)
37-
document.add_paragraph('A plain paragraph.')
38-
document.add_heading('Heading, level 1', level=1)
39-
document.add_paragraph('Intense quote', style='IntenseQuote')
40-
document.add_bullet('first item in unordered list')
41-
document.add_list_item('first item in ordered list')
44+
document.add_picture('monty-truth.png')
4245

43-
document.add_picture('monty-truth.png')
46+
table = document.add_table(rows=1, cols=3)
47+
table.style = 'LightShading-Accent1'
48+
header_cells = table.rows[0].cells
49+
header_cells[0].text = 'Qty'
50+
header_cells[1].text = 'Id'
51+
header_cells[2].text = 'Desc'
52+
for item in recordset:
53+
row_cells = table.add_row().cells
54+
row_cells[0].text = str(item.qty)
55+
row_cells[1].text = str(item.id)
56+
row_cells[2].text = item.desc
4457

45-
table = document.add_table(rows=1, cols=3)
46-
table.style = 'LightShading-Accent1'
47-
header_cells = table.rows[0].cells
48-
header_cells[0].text = 'Qty'
49-
header_cells[1].text = 'Id'
50-
header_cells[2].text = 'Desc'
51-
for item in recordset:
52-
row_cells = table.add_row().cells
53-
row_cells[0].text = str(item.qty)
54-
row_cells[1].text = str(item.id)
55-
row_cells[2].text = item.desc
56-
57-
document.add_page_break()
58+
document.add_page_break()
5859

59-
document.save('demo.docx')
60+
document.save('demo.docx')
61+
====== ======================================================================
6062

6163

6264
User Guide
@@ -70,6 +72,7 @@ User Guide
7072
user/api-concepts
7173
user/styles
7274
user/tables
75+
user/shapes
7376
user/text
7477

7578

docs/user/shapes.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Understanding pictures and other shapes
3+
=======================================
4+
5+
Conceptually, Word documents have two *layers*, a *text layer* and a *drawing
6+
layer*. In the text layer, text objects are flowed from left to right and from
7+
top to bottom, starting a new page when the prior one is filled. In the drawing
8+
layer, drawing objects, called *shapes*, are placed at arbitrary positions.
9+
and are sometimes referred to as *floating* shapes.
10+
11+
A picture is a shape that can appear in either the text or drawing layer. When
12+
it appears in the text layer it is called an *inline shape*, or more
13+
specifically, an *inline picture*.
14+
15+
Inline shapes are treated like a big text character (a *character glyph*). The
16+
line height is increased to accomodate the shape and the shape is wrapped to
17+
a line it will fit on width-wise, just like text. Inserting text in front of it
18+
will cause it to move to the right. Often, a picture is placed in a paragraph
19+
by itself, but this is not required. It can have text before and after it in
20+
the paragraph in which it's placed.
21+
22+
At the time of writing, |docx| only supports inline pictures. Floating pictures
23+
can be added. If you have an active use case, submit a feature request on the
24+
issue tracker. The ``Document.add_picture()`` method adds a specified picture
25+
to the end of the document in a paragraph of its own. However, by digging
26+
a little deeper into the API you can place text on either side of the picture
27+
in its paragraph, or both.

docx/api.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
class Document(object):
2020
"""
21-
API class representing a Word document.
21+
Return a |Document| instance loaded from *docx*, where *docx* can be
22+
either a path to a ``.docx`` file (a string) or a file-like object. If
23+
*docx* is missing or ``None``, the built-in default document "template"
24+
is loaded.
2225
"""
2326
def __init__(self, docx=None):
2427
super(Document, self).__init__()
@@ -48,12 +51,12 @@ def inline_shapes(self):
4851
"""
4952
return self._document_part.inline_shapes
5053

51-
def save(self, file_):
54+
def save(self, path_or_stream):
5255
"""
53-
Save this document to *file_*, where *file_* can be either a path to
54-
a file (a string) or a file-like object.
56+
Save this document to *path_or_stream*, which can be either a path to
57+
a filesystem location (a string) or a file-like object.
5558
"""
56-
self._package.save(file_)
59+
self._package.save(path_or_stream)
5760

5861
@staticmethod
5962
def _open(docx):

0 commit comments

Comments
 (0)