三.Working with Sections
Word支持section的概念,即文档的一个部分,具有相同的页面布局设置,如页边距和页面方向。例如,这就是文档在纵向布局中包含某些页面而在横向布局中包含其他页面的方式。大多数Word文档只有一个默认的部分,而且大多数文档没有理由改变默认的页边距或其他页面布局。但是,当您确实需要更改页面布局时,您需要了解各个部分才能完成。
1.获取sections对象
sections = document.sections #(from docx import section)
2.添加新的部分
add_section()方法允许在文档末尾启动一个新节。调用此方法后添加的段落和表将出现在新的部分:
current_section = document.sections[-1] # last section in document
new_section = document.add_section(WD_SECTION.ODD_PAGE)
new_section.start_type
3.Section properties属性
Section对象有11个属性,允许发现和指定页面布局设置。
Section start type部分启动类型
section.start_type起始类型new_page(2),然后 section.start_type = WD_SECTION.ODD_PAGE
ODD_PAGE (4)>>>>>>>start_type的值是WD_SECTION_START枚举的成员。
Page dimensions and orientation页面尺寸和方向
Section中的三个属性描述了页面的大小和方向。这些可以一起使用,例如,改变一个截面的方向从纵向到横向:
用到了:section.orientation, section.page_width, section.page_height
>>>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)
View Code
Page margins页边距
Section上的七个属性一起指定了决定文本在页面上出现位置的各种边缘间距:
#section.left_margin, section.right_margin
#section.top_margin, section.bottom_margin
#section.gutter
#section.header_distance, section.footer_distance
1 from docx.shared importInches2 #section.left_margin, section.right_margin
3 #section.top_margin, section.bottom_margin
4 #section.gutter
5 #section.header_distance, section.footer_distance
6
7 section.left_margin = Inches(1.5)8 section.right_margin = Inches(1)9 section.left_margin, section.right_margin
Working with Headers and Footers 处理页眉和页脚
Word支持页眉和页脚。页眉是出现在每页顶部空白区域的文本,与正文分离,通常传递上下文信息,如文档标题、作者、创建日期或页码。文档中的页眉在页与页之间是相同的,只有内容上的细微差异,比如更改了节标题或页码。页眉也称为运行头。页脚在任何方面都类似于页眉,但它出现在页面的底部。它不应该被混淆
Accessing the header for a section访问节的标头
每个section对象都有一个.header属性,为该section提供对_Header对象的访问:
Adding a header (simple case)添加标题(简单的情况)
paragraph = header.paragraphs[0]
paragraph.text = "Title of my document"
Adding “zoned” header content添加“分区”标题内容
带有多个“区域”的页眉通常是使用精心放置的制表符来完成的。中心对齐和右对齐的“区域”所需的制表符停止是Word中页眉和页脚样式的一部分。如果您使用的是自定义模板而不是python-docx默认模板,那么在模板中定义该样式可能是有意义的。插入的制表符(“\t”)用于分隔左、中、右对齐的标题内容:
>>> paragraph = header.paragraphs[0]
>>> paragraph.text = "Left Text\tCenter Text\tRight Text"
>>> paragraph.style = document.styles["Header"]
Removing a header
header.is_linked_to_previous = True
#换页只需要把docx.enum.text.WD_BREAK.PAGE作为唯一的参数传递给add_break
#如果是换行的话就不需要使用docx.enum.text.WD_BREAK.PAGE参数
doc2.paragraphs[0].runs[0].add_break(docx.enum.text.WD_BREAK.PAGE)
四.Styles
Questions:
What is a style in Word?
Why doesn’t the style I applied show up?
Access a style
The Styles object is also iterable. By using the identification properties on BaseStyle, various subsets of the defined styles can be generated. For example, this code will produce a list of the defined paragraph styles:Styles对象也是可迭代的。通过使用BaseStyle上的标识属性,可以生成定义的样式的各种子集。例如,这段代码将生成定义的段落样式列表:
>>> from docx.enum.style importWD_STYLE_TYPE>>> styles =document.styles>>> paragraph_styles =[
... sfor s in styles if s.type ==WD_STYLE_TYPE.PARAGRAPH
... ]>>> for style inparagraph_styles:
...print(style.name)
...
Normal
Body Text
List Bullet
View Code
Apply a style应用一个样式
>>> document =Document()>>> paragraph =document.add_paragraph()>>>paragraph.style
>>>paragraph.style.name'Normal'
>>> paragraph.style = document.styles['Heading 1']>>>paragraph.style.name'Heading 1'
View Code
Add or delete a style
#通过指定一个唯一的名称和样式类型,可以将一个新样式添加到文档中:
>>> from docx.enum.style importWD_STYLE_TYPE>>> styles =document.styles>>> style = styles.add_style('Citation', WD_STYLE_TYPE.PARAGRAPH)>>>style.name'Citation'
>>>style.type
PARAGRAPH (1)#使用base_style属性指定新样式应该继承的格式设置:
>>>style.base_style
None>>> style.base_style = styles['Normal']>>>style.base_style
>>>style.base_style.name'Normal'
#一个样式可以从文档中删除,只需调用它的delete()方法:
>>> styles =document.styles>>>len(styles)10
>>> styles['Citation'].delete()>>>len(styles)9