通过python-docx的章节属性,就可以更改纸张方向、纸张尺寸。

import docxfrom docx.enum.section import WD_ORIENTfrom docx.shared import Cmdocument = docx.Document()section = document.sections[0]# 设置纸张大小为A4大小section.page_width = Cm(21)section.page_height = Cm(29.7)# 设置纸张方向横向,横向是LANDSCAPE,竖向是PORTRAITsection.orientation = WD_ORIENT.LANDSCAPE# 设置章节宽高,也就是宽高互换section.page_width, section.page_height = section.page_height, section.page_widthdocument.save('landscape.docx')

更改纸张方向,分两步,第一步是设置section的orientation属性为LANDSCAPE,第二步是设置section的宽高互换。

相关链接

页面尺寸和方向