首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在python-docx中将页面大小更改为A4

如何在python-docx中将页面大小更改为A4
EN

Stack Overflow用户
提问于 2017-05-02 02:06:33
回答 3查看 7.4K关注 0票数 12

我尝试使用python-docx创建Word文档。创建的文件的字母尺寸为8.5x11英寸。但在德国,标准格式是A4 8.27x11.69英寸。

代码语言:javascript
运行
复制
from docx import Document
from docx.shared import Inches

document = Document()

document.add_heading('Document Title', 0)
document.settings


p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True

document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='IntenseQuote')

document.add_paragraph(
    'first item in unordered list', style='ListBullet'
)
document.add_paragraph(
    'first item in ordered list', style='ListNumber'
)



table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'


document.add_page_break()

document.save('demo.docx')

我在文档中找不到任何关于这个主题的信息。

EN

回答 3

Stack Overflow用户

发布于 2019-02-19 08:29:21

代码语言:javascript
运行
复制
from docx.shared import Mm

document = Document()
section = document.sections[0]
section.page_height = Mm(297)
section.page_width = Mm(210)
section.left_margin = Mm(25.4)
section.right_margin = Mm(25.4)
section.top_margin = Mm(25.4)
section.bottom_margin = Mm(25.4)
section.header_distance = Mm(12.7)
section.footer_distance = Mm(12.7)
票数 9
EN

Stack Overflow用户

发布于 2017-05-02 02:22:56

看起来Document是由几个具有page_heightpage_width属性的Section组成的。

要将第一部分的尺寸设置为A4,您可以尝试(未测试):

代码语言:javascript
运行
复制
section = document.sections[0]
section.page_height = Mm(297)
section.page_width = Mm(210)

注意,A4是在millimeters中定义的。

票数 6
EN

Stack Overflow用户

发布于 2017-05-02 02:15:53

我相信你想要这个,从documentation那里。

节上的三个属性描述页面尺寸和方向。将这些一起使用,例如可以将截面的方向从纵向改变为纵向,section.page_height ( landscape:section.orientation,(0),7772400,10058400) #(英寸(7772400),英寸(11)) 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 >>> section.orientation,,横向(横向(1),10058400,7772400)

,section.page_height

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43724030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档