概述
python-docx可以对word进行一些设置,比如布局等等,全部设置的话过于繁琐,之前python docxtpl可以直接通过word设置,比较方便,不过若是有些不得不用代码设置的需求,可以考虑以下方法。
这里引入一个概念section,即为节,章节,章节的范围比段落paragraph要大。这部分的设置对应于word中的页面布局,如下图
可以设置页眉的纸张方向,页面边距,页面大小,每个节可以设置单独的页眉页脚,也可以设置其页面布局,比如横向纵向。
纸张方向
from docx import Document
from docx.enum.section import WD_ORIENT
doc = Document('xxx.docx')
sections = doc.sections
for section in sections:
new_width, new_height = section.page_height, section.page_width
sec