Hướng dẫn python-docx set page margin

I need to quickly change the margins of many docx documents. I checked python-docx and I do not find a way to access/modify the page layout (in particular the margins) properties. Is there a way?

asked Oct 2, 2015 at 19:36

2

Thanks to @tdelaney for pointing out the page where it clearly indicated the solution. I am just posting here the code I used in case anyone else is confused as I initially was:

#Open the document
document = Document(args.inputFile)

#changing the page margins
sections = document.sections
for section in sections:
    section.top_margin = Cm(margin)
    section.bottom_margin = Cm(margin)
    section.left_margin = Cm(margin)
    section.right_margin = Cm(margin)

document.save(args.outputFile)

answered Oct 2, 2015 at 21:55

XAngueraXAnguera

1,1071 gold badge11 silver badges23 bronze badges

4

import docx
from docx.shared import Inches, Cm
doc = docx.Document()
sections = doc.sections
for section in sections:
    section.top_margin = Cm(0.5)
    section.bottom_margin = Cm(0.5)
    section.left_margin = Cm(1)
    section.right_margin = Cm(1)

Here's the code that i used please include from docx.shared import Inches, Cm

answered Jun 17, 2020 at 13:50

Hướng dẫn python-docx set page margin

Simon Björk

unread,

Apr 9, 2015, 4:50:22 AM4/9/15

to

Hello,

I have a simple document where I add a bit of text at the top of the page and then add a table. This all works great, except for the alignment to the page margins. I would like the text at the top to be vertically aligned with the border of the table. As it is now, the text at the top is aligned with the text in the table, but the actual boarder of the table is a bit further to the left. Is this possible?

Example code:

from docx import Document
from docx.shared import Cm

document

= Document()

document

.add_paragraph().add_run("temp text")

table

= document.add_table(rows=4, cols=4)
table
.style = 'TableGrid'
table
.rows[0].cells[0].paragraphs[0].add_run("more text")

section

= document.sections[0]
section
.left_margin = Cm(1.25)
section
.top_margin = Cm(1.0)

document

.save("tmp.docx")

Simon Björk

unread,

Apr 9, 2015, 4:55:31 AM4/9/15

to

By the way, I get an error when I add the style to the table. Is this expected?

/usr/local/lib/python2.7/site-packages/docx/styles/styles.py:54: UserWarning: style lookup by style_id is deprecated. Use style name as key instead.
  warn
(msg, UserWarning)

Steve Canny

unread,

Apr 9, 2015, 4:21:40 PM4/9/15

to

What style name do you provide?

--Steve

Simon Björk

unread,

Apr 9, 2015, 11:00:35 PM4/9/15

to

table = document.add_table(rows=4, cols=4)
table
.style = 'TableGrid'

Steve Canny

unread,

Apr 10, 2015, 1:59:45 AM4/10/15

to

Since version 0.8.0, you need to use the style name to specify a style, rather than its (potentially changing) style id.

Generally, the style id is the style name with spaces removed (also possibly dashes and/or underscores).

So if you use "Table Grid" instead of "TableGrid", I think it will work for you.

If not, you can examine the styles.xml part of the .docx file using opc-diag to see what the name is. Alternatively you could enumerate the names with something like this:

for style in document.styles:

--Steve

Simon Björk

unread,

Apr 10, 2015, 7:24:45 AM4/10/15

to

Thanks Steve, that removed the error. Great :).

Any suggestion on the original post?

Steve Canny

unread,

Apr 22, 2015, 3:59:54 AM4/22/15

to

There's no property implemented for that yet, I think it would be something like Table.indent, I'd have to research it a bit.

Anyway, you might be able to get it done with a table style, like define a table style with that attribute set in your starting document, then apply that style to the table.

--Steve