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

如何在python docx中将页面大小更改为A4,python,docx,python-docx,Python,Docx,Python Docx,我尝试使用pythondocx创建Word文档。创建的文件的字母尺寸为8.5 x 11英寸。但在德国,标准格式是A4 8.27 x 11.69英寸 from docx import Document from docx.shared import Inches document = Document() document.add_heading('Document Title', 0) document.settings p = document.add_paragraph('A pla

我尝试使用pythondocx创建Word文档。创建的文件的字母尺寸为8.5 x 11英寸。但在德国,标准格式是A4 8.27 x 11.69英寸

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')

我在文档中找不到有关此主题的任何信息。

我相信您希望从

第节中的三个属性描述页面尺寸和方向。 例如,可以将这些元素组合在一起用于更改对象的方向 从纵向到横向的部分:

>>> section.orientation, section.page_width, section.page_height
(PORTRAIT (0), 7772400, 10058400)  # (Inches(8.5), Inches(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, section.page_width, section.page_height
(LANDSCAPE (1), 10058400, 7772400)

一个
文档
似乎由几个具有
页面高度
页面宽度
属性的s组成

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


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

非常感谢!。我浪费了几个小时试图找出如何设置页边距
section = document.sections[0]
section.page_height = Mm(297)
section.page_width = Mm(210)
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)