加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python Docx – 部分 – 页面方向

发布时间:2020-12-20 12:08:51 所属栏目:Python 来源:网络整理
导读:以下代码尝试使用横向方向,但文档创建为potrait.你能说出问题出在哪里吗? from docx import Documentfrom docx.enum.section import WD_ORIENTdocument = Document()section = document.sections[-1]section.orientation = WD_ORIENT.LANDSCAPEdocument.add
以下代码尝试使用横向方向,但文档创建为potrait.你能说出问题出在哪里吗?

from docx import Document
from docx.enum.section import WD_ORIENT

document = Document()

section = document.sections[-1]
section.orientation = WD_ORIENT.LANDSCAPE

document.add_heading('text')
document.save('demo.docx')

当我以XML格式读回代码时

<w:document>
    <w:body>
       <w:p>
          <w:pPr>
             <w:pStyle w:val="Heading1"/>
          </w:pPr>
          <w:r>
              <w:t>TEXT</w:t>
          </w:r>
       </w:p>
       <w:sectPr w:rsidR="00FC693F" w:rsidRPr="0006063C" w:rsidSect="00034616">
           <w:pgSz w:w="12240" w:h="15840" w:orient="landscape"/>
           <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="720" w:footer="720" w:gutter="0"/>
           <w:cols w:space="720"/>
           <w:docGrid w:linePitch="360"/>
        </w:sectPr>
    </w:body>
 </w:document>

假设段标签应该位于顶部而不是底部的TEXT标签之上,我不太清楚XML.

解决方法

虽然页面被正确标记为横向,但其尺寸仍与以前相同,必须手动更改.

http://python-docx.readthedocs.io/en/latest/user/sections.html

Page dimensions and orientation

Three properties on Section describe page dimensions and orientation. Together these can be used,for example,to change the orientation of a section from portrait to landscape:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读