此篇博客将从两个方面讲清楚如何使用Python-docx库将docx文档设置为横向
第一种,设置当前页面方向为横线
from docx import Document
from docx.enum.section import WD_ORIENT
section = document.sections[0]
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
document.save('test3.docx')
第二种,设置所有章节的页面方向均为横向
from docx import Document
from docx.enum.section import WD_ORIENT
sections = document.sections
for section in sections:
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
document.save('test2.docx')
第三种,分别设置为每一章节的纸张方向,处理结果为:第一章节为纵向,第二章节为横向,第三章节为纵向
from docx import Document
from docx.enum.section import WD_ORIENTATION, WD_SECTION_START
document = Document()
document.add_paragraph()
section = document.add_section(start_type=WD_SECTION_START.CONTINUOUS)
section.orientation = WD_ORIENTATION.LANDSCAPE
page_h, page_w = section.page_width, section.page_height
section.page_width = page_w
section.page_height = page_h
document.add_paragraph()
section = document.add_section(start_type=WD_SECTION_START.CONTINUOUS)
section.orientation = WD_ORIENTATION.PORTRAIT
page_h, page_w = section.page_width, section.page_height
section.page_width = page_w
section.page_height = page_h
document.save('test.docx')
|