X Tutup
Skip to content

Commit 164b4af

Browse files
author
Steve Canny
committed
docs: add user documentation for sections
1 parent e464e22 commit 164b4af

File tree

13 files changed

+242
-57
lines changed

13 files changed

+242
-57
lines changed

docs/api/document.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Document objects
55
================
66

7-
... things having to do with Document objects ...
7+
The main Document and related objects.
88

99

1010
.. currentmodule:: docx.api
@@ -16,3 +16,15 @@ Document objects
1616

1717
.. autoclass:: Document
1818
:members:
19+
:exclude-members: numbering_part, styles_part
20+
21+
22+
.. currentmodule:: docx.parts.document
23+
24+
25+
|Sections| objects
26+
------------------
27+
28+
29+
.. autoclass:: Sections
30+
:members:

docs/api/enum/WdOrientation.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.. _WdOrientation:
2+
3+
``WD_ORIENTATION``
4+
==================
5+
6+
alias: **WD_ORIENT**
7+
8+
Specifies the page layout orientation.
9+
10+
Example::
11+
12+
from docx.enum.section import WD_ORIENT
13+
14+
section = document.sections[-1]
15+
section.orientation = WD_ORIENT.LANDSCAPE
16+
17+
----
18+
19+
PORTRAIT
20+
Portrait orientation.
21+
22+
LANDSCAPE
23+
Landscape orientation.

docs/api/enum/WdSectionStart.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.. _WdSectionStart:
2+
3+
``WD_SECTION_START``
4+
====================
5+
6+
alias: **WD_SECTION**
7+
8+
Specifies the start type of a section break.
9+
10+
Example::
11+
12+
from docx.enum.section import WD_SECTION
13+
14+
section = document.sections[0]
15+
section.start_type = WD_SECTION.NEW_PAGE
16+
17+
----
18+
19+
CONTINUOUS
20+
Continuous section break.
21+
22+
NEW_COLUMN
23+
New column section break.
24+
25+
NEW_PAGE
26+
New page section break.
27+
28+
EVEN_PAGE
29+
Even pages section break.
30+
31+
ODD_PAGE
32+
Section begins on next odd page.

docs/api/enum/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ can be found here:
88
.. toctree::
99
:titlesonly:
1010

11+
WdOrientation
12+
WdSectionStart
1113
WdUnderline

docs/api/section.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
.. _section_api:
3+
4+
Section objects
5+
===============
6+
7+
Provides access to section properties such as margins and page orientation.
8+
9+
10+
.. currentmodule:: docx.section
11+
12+
13+
|Section| objects
14+
-----------------
15+
16+
17+
.. autoclass:: Section
18+
:members:

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
7878
.. |_Columns| replace:: :class:`_Columns`
7979
80-
.. |Document| replace:: :class:`Document`
80+
.. |Document| replace:: :class:`.Document`
8181
8282
.. |docx| replace:: ``python-docx``
8383
@@ -117,7 +117,7 @@
117117
118118
.. |Sections| replace:: :class:`.Sections`
119119
120-
.. |StylesPart| replace:: :class:`StylesPart`
120+
.. |StylesPart| replace:: :class:`.StylesPart`
121121
122122
.. |Table| replace:: :class:`.Table`
123123

docs/dev/analysis/features/sections.rst

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,54 +32,6 @@ occur:
3232
element is changed to reflect the type chosen by the user from the UI.
3333

3434

35-
Candidate protocol
36-
------------------
37-
38-
The following interactive session demonstrates the proposed protocol for
39-
inserting a section break, illustrating adding a landscape section after an
40-
existing portrait section::
41-
42-
>>> section_1 = document.sections[0]
43-
>>> document.add_paragraph('This paragraph appears in section 1.')
44-
>>> section_2 = document.add_section(WD_SECTION.EVEN_PAGE)
45-
>>> section_2.orientation
46-
PORTRAIT (0)
47-
>>> section_2.orientation = WD_ORIENT.LANDSCAPE
48-
>>> section_2.page_width = section_1.page_height
49-
>>> section_2.page_height = section_1.page_width
50-
>>> document.add_paragraph('This paragraph appears in section 2.')
51-
52-
53-
The following interactive session demonstrates the proposed protocol for
54-
setting section properties::
55-
56-
>>> sections = document.sections
57-
>>> sections
58-
<docx.parts.document.Sections object at 0x1deadbeef>
59-
>>> len(sections)
60-
3
61-
>>> section = sections[-1] # the sentinel section
62-
>>> section
63-
<docx.section.Section object at 0x1deadbeef>
64-
>>> section.section_start
65-
WD_SECTION.CONTINUOUS (0)
66-
>>> page_setup = section.page_setup
67-
>>> page_setup
68-
<docx.section.PageSetup object at 0x1deadbeef>
69-
>>> page_setup.page_width
70-
7772400 # Inches(8.5)
71-
>>> page_setup.page_height
72-
10058400 # Inches(11)
73-
>>> page_setup.orientation
74-
WD_ORIENT.PORTRAIT
75-
>>> page_setup.left_margin # and .right_, .top_, .bottom_
76-
914400
77-
>>> page_setup.header_distance # and .footer_distance
78-
457200 # Inches(0.5)
79-
>>> page_setup.gutter
80-
0
81-
82-
8335
Word behavior
8436
-------------
8537

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ User Guide
6767
user/install
6868
user/quickstart
6969
user/documents
70+
user/sections
7071
user/api-concepts
7172
user/styles
7273
user/shapes
@@ -82,6 +83,7 @@ API Documentation
8283
api/document
8384
api/table
8485
api/text
86+
api/section
8587
api/shape
8688
api/shared
8789
api/enum/index

docs/user/sections.rst

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
.. _sections:
2+
3+
Working with Sections
4+
=====================
5+
6+
Word supports the notion of a *section*, a division of a document having the
7+
same page layout settings, such as margins and page orientation. This is how,
8+
for example, a document can contain some pages in portrait layout and others in
9+
landscape.
10+
11+
Most Word documents have only the single section that comes by default and
12+
further, most of those have no reason to change the default margins or other
13+
page layout. But when you *do* need to change the page layout, you'll need
14+
to understand sections to get it done.
15+
16+
17+
Accessing sections
18+
------------------
19+
20+
Access to document sections is provided by the ``sections`` property on the
21+
|Document| object::
22+
23+
>>> document = Document()
24+
>>> sections = document.sections
25+
>>> sections
26+
<docx.parts.document.Sections object at 0x1deadbeef>
27+
>>> len(sections)
28+
3
29+
>>> section = sections[0]
30+
>>> section
31+
<docx.section.Section object at 0x1deadbeef>
32+
>>> for section in sections:
33+
... print(section.start_type)
34+
...
35+
NEW_PAGE (2)
36+
EVEN_PAGE (3)
37+
ODD_PAGE (4)
38+
39+
It's theoretically possible for a document not to have any explicit sections,
40+
although I've yet to see this occur in the wild. If you're accessing an
41+
unpredictable population of .docx files you may want to provide for that
42+
possibility using a ``len()`` check or ``try`` block to avoid an uncaught
43+
``IndexError`` exception stopping your program.
44+
45+
46+
Adding a new section
47+
--------------------
48+
49+
.. currentmodule:: docx.api
50+
51+
The :meth:`Document.add_section` method allows a new section to be started at
52+
the end of the document. Paragraphs and tables added after calling this method
53+
will appear in the new section::
54+
55+
>>> current_section = document.section[-1] # last section in document
56+
>>> current_section.start_type
57+
NEW_PAGE (2)
58+
>>> new_section = document.add_section(WD_SECTION.ODD_PAGE)
59+
>>> new_section.start_type
60+
ODD_PAGE (4)
61+
62+
63+
Section properties
64+
------------------
65+
66+
.. currentmodule:: docx.section
67+
68+
The |Section| object has eleven properties that allow page layout settings to
69+
be discovered and specified.
70+
71+
72+
Section start type
73+
~~~~~~~~~~~~~~~~~~
74+
75+
:attr:`Section.start_type` describes the type of break that precedes the
76+
section::
77+
78+
>>> section.start_type
79+
NEW_PAGE (2)
80+
>>> section.start_type = WD_SECTION.ODD_PAGE
81+
>>> section.start_type
82+
ODD_PAGE (4)
83+
84+
Values of ``start_type`` are members of the :ref:`WdSectionStart` enumeration.
85+
86+
87+
Page dimensions and orientation
88+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89+
90+
Three properties on |Section| describe page dimensions and orientation.
91+
Together these can be used, for example, to change the orientation of a section
92+
from portrait to landscape::
93+
94+
>>> section.orientation, section.page_width, section.page_height
95+
(PORTRAIT (0), 7772400, 10058400) # (Inches(8.5), Inches(11))
96+
>>> new_width, new_height = section.page_height, section.page_width
97+
>>> section.orientation = WD_ORIENT.LANDSCAPE
98+
>>> section.page_width = new_width
99+
>>> section.page_height = new_height
100+
>>> section.orientation, section.page_width, section.page_height
101+
(LANDSCAPE (1), 10058400, 7772400)
102+
103+
104+
Page margins
105+
~~~~~~~~~~~~
106+
107+
Seven properties on |Section| together specify the various edge spacings that
108+
determine where text appears on the page::
109+
110+
>>> from docx.shared import Inches
111+
>>> section.left_margin, section.right_margin
112+
(1143000, 1143000) # (Inches(1.25), Inches(1.25))
113+
>>> section.top_margin, section.bottom_margin
114+
(914400, 914400) # (Inches(1), Inches(1))
115+
>>> section.gutter
116+
0
117+
>>> section.header_distance, section.footer_distance
118+
(457200, 457200) # (Inches(0.5), Inches(0.5))
119+
>>> section.left_margin = Inches(1.5)
120+
>>> section.right_margin = Inches(1)
121+
>>> section.left_margin, section.right_margin
122+
(1371600, 914400)

docx/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ def add_picture(self, image_path_or_stream, width=None, height=None):
104104
def add_section(self, start_type=WD_SECTION.NEW_PAGE):
105105
"""
106106
Return a |Section| object representing a new section added at the end
107-
of the document.
107+
of the document. The optional *start_type* argument must be a member
108+
of the :ref:`WdSectionStart` enumeration defaulting to
109+
``WD_SECTION.NEW_PAGE`` if not provided.
108110
"""
109111
return self._document_part.add_section(start_type)
110112

0 commit comments

Comments
 (0)
X Tutup