python将Word页面纸张方向设置为横向

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

python 复制代码
import docx
from docx.enum.section import WD_ORIENT
from docx.shared import Cm

document = docx.Document()
section = document.sections[0]

# 设置纸张大小为A4大小
section.page_width = Cm(21)
section.page_height = Cm(29.7)

# 设置纸张方向横向,横向是LANDSCAPE,竖向是PORTRAIT
section.orientation = WD_ORIENT.LANDSCAPE

# 设置章节宽高,也就是宽高互换
section.page_width, section.page_height = section.page_height, section.page_width
document.save('landscape.docx')

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

相关链接

页面尺寸和方向

相关推荐
是烟花哈18 分钟前
【前端】React框架学习
前端·学习·react.js
qq4356947011 小时前
JavaWeb08
前端
2401_878454532 小时前
html和css的复习(1)
前端·css·html
@PHARAOH2 小时前
WHAT - git worktree 概念
前端·git
IT_陈寒3 小时前
我竟然被JavaScript的隐式类型转换坑了三天!
前端·人工智能·后端
我亚索贼六丶3 小时前
二十六. AI基础概念之如何更好的使用AI
前端
小码哥_常3 小时前
安卓启动页Logo适配秘籍:告别“奇形怪状”的展示
前端
我亚索贼六丶3 小时前
二十五.Electron 初体验与进阶
前端
当时只道寻常3 小时前
像使用 Redis 一样操作 LocalStorage
前端·前端工程化
RONIN3 小时前
UI组件库elementplus
前端