How to set inside and outside margins

89 Views Asked by At

I am using python-docx to create a Word .docx in python and I would like to set the inside and outside margins as well as the gutter. How to do so?

Given a section I can set the left and right margins like this:

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

But how to set the inside and outside ones?

1

There are 1 best solutions below

0
Achraf Ben Salah On BEST ANSWER

You can try like this :

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

I adjusted the top, bottom, left, and right margins by accessing the section.top_margin, section.bottom_margin, section.left_margin, and section.right_margin properties, respectively. To configure the inside and outside margins, I utilized the section.inside_margin and section.outside_margin properties. Lastly, I specified the gutter margin by utilizing the section.gutter property.