2020-07_python-docx 0.8.10 User Guide_05_Working with Sections

python-docx 0.8.10 User Guide

说明:

  1. 本文的内容来源python-docx官方文档,出于学习的目的借助于有道进行翻译。
  2. 翻译的原则:只有在有道翻译出的内容不通顺或意义有误的情况下才使用个人翻译。

Working with Sections

Word supports the notion of a section, a division of a document having the same page layout settings, such as margins and page orientation. This is how, for example, a document can contain some pages in portrait layout and others in landscape.
Word支持section的概念,即具有相同页面布局设置(如页边距和页面方向)的文档的一个分区。例如,这就是文档如何包含一些纵向布局的页面和一些横向布局的页面。

Most Word documents have only the single section that comes by default and further, most of those have no reason to change the default margins or other page layout. But when you do need to change the page layout, you’ll need to understand sections to get it done.
大多数Word文档只有一个默认的部分,而且大多数没有理由改变默认的页边距或其他页面布局。但是当你确实需要改变页面布局时,你需要理解章节来完成它。

Accessing sections 访问部分

Access to document sections is provided by the sections property on the Document object:
对文档节的访问由文档对象上的section属性提供:

>>> document = Document()
>>> sections = document.sections
>>> sections
<docx.parts.document.Sections object at 0x1deadbeef>
>>> len(sections)
3
>>> section = sections[0]
>>> section
<docx.section.Section object at 0x1deadbeef>
>>> for section in sections:
...     print(section.start_type)
...
NEW_PAGE (2)
EVEN_PAGE (3)
ODD_PAGE (4)

It’s theoretically possible for a document not to have any explicit sections, although I’ve yet to see this occur in the wild. If you’re accessing an unpredictable population of .docx files you may want to provide for that possibility using a len() check or try block to avoid an uncaught IndexError exception stopping your program.
从理论上讲,文档不具有任何明确的节是可能的,尽管我还没有看到这种情况发生。如果你正在访问不可预测的.docx文件,你可能想要提供这种可能性使用len()检查或try块,以避免一个未捕获的IndexError异常停止你的程序。

Adding a new section

The Document.add_section() method allows a new section to be started at the end of the document. Paragraphs and tables added after calling this method will appear in the new section:
add_section()方法允许在文档的末尾开始一个新的section。调用此方法后添加的段落和表将出现在新部分中:

>>> current_section = document.sections[-1]  # last section in document
>>> current_section.start_type
NEW_PAGE (2)
>>> new_section = document.add_section(WD_SECTION.ODD_PAGE)
>>> new_section.start_type
ODD_PAGE (4)
Section properties

The Section object has eleven properties that allow page layout settings to be discovered and specified.
Section对象有十一个允许发现和指定页面布局设置的属性。

Section start type

Section.start_type describes the type of break that precedes the section:
Section.start_type描述了片段前面的中断类型:

>>> section.start_type
NEW_PAGE (2)
>>> section.start_type = WD_SECTION.ODD_PAGE
>>> section.start_type
ODD_PAGE (4)

Values of start_type are members of the WD_SECTION_START enumeration.

Page dimensions and orientation
页面尺寸和方向

Three properties on Section describe page dimensions and orientation. Together these can be used, for example, to change the orientation of a section from portrait to landscape:
Section 的三个属性描述了页面的尺寸和方向。这些可以一起使用,例如,改变一个部分的方向,从纵向到横向:

>>> section.orientation, section.page_width, section.page_height
(PORTRAIT (0), 7772400, 10058400)  # (Inches(8.5), Inches(11))
>>> new_width, new_height = section.page_height, section.page_width
>>> section.orientation = WD_ORIENT.LANDSCAPE
>>> section.page_width = new_width
>>> section.page_height = new_height
>>> section.orientation, section.page_width, section.page_height
(LANDSCAPE (1), 10058400, 7772400)

Page margins 边距
Seven properties on Section together specify the various edge spacings that determine where text appears on the page:
Section 的7个属性一起指定不同的边间距,决定文本在页面上出现的位置:

>>> from docx.shared import Inches
>>> section.left_margin, section.right_margin
(1143000, 1143000)  # (Inches(1.25), Inches(1.25))
>>> section.top_margin, section.bottom_margin
(914400, 914400)  # (Inches(1), Inches(1))
>>> section.gutter
0
>>> section.header_distance, section.footer_distance
(457200, 457200)  # (Inches(0.5), Inches(0.5))
>>> section.left_margin = Inches(1.5)
>>> section.right_margin = Inches(1)
>>> section.left_margin, section.right_margin
(1371600, 914400)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值