python - 如何设置内边距和外边距

标签 python ms-word python-docx margins

我正在使用 python-docxpython 中创建一个 Word .docx 并且我想设置 insideoutside 边距以及 gutter。如何做到这一点?

给定一个部分,我可以像这样设置左右边距:

section.top_margin = Mm(6.6)
section.bottom_margin = Mm(6.6)

但是内部和外部如何设置呢?

最佳答案

你可以这样尝试:

from docx import Document
from docx.shared import Mm

# Create a new Document
doc = Document()

# Add a section to the document
section = doc.sections[0]

# Set the top, bottom, left, and right margins
section.top_margin = Mm(6.6)
section.bottom_margin = Mm(6.6)
section.left_margin = Mm(6.6)
section.right_margin = Mm(6.6)

# Set the inside and outside margins
section.inside_margin = Mm(12.7)  # Inside margin (towards the binding)
section.outside_margin = Mm(12.7)  # Outside margin (away from the binding)

# Set the gutter margin (gap between the inside margins of facing pages)
section.gutter = Mm(6.6)

# Save the document
doc.save("your_document.docx")

我通过访问 section.top_marginsection.bottom_marginsection.left_margin 调整了上、下、左、右页边距和 section.right_margin 属性,分别。为了配置内部和外部边距,我使用了 section.inside_marginsection.outside_margin 属性。最后,我利用 section.gutter 属性指定了装订线边距。

关于python - 如何设置内边距和外边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77142499/

相关文章:

python - 您是否可以仅修改 XML 文件中的文本字符串并仍然保持 .docx 封装的完整性和功能?

python - 类实例变量继承和类变量继承

Python如何覆盖第三方库模块中定义的方法

java - 从 Word (Docx) 读取方程和公式

c# - 从 MS-Word ApplicationClass 获取 PID?

python - 如何使用 python 的 docxtpl 包生成多个 docx 文件,从而保留 docx 格式?

python - 使用 ipython notebook 生成工程计算

Python:如何使用存储在变量中的值?

javascript - Ooxml 中不显示下划线

python - 如何查看 python-docx 包生成的 XML