|
| 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) |
0 commit comments