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:
answered Oct 2, 2015 at 21:55
XAngueraXAnguera 1,1071 gold badge11 silver badges23 bronze badges 4
Here's the code that i used please include from docx.shared import Inches, Cm answered Jun 17, 2020 at 13:50
Simon Björkunread, 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:
= Document() Simon Björkunread, 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?
Steve Cannyunread, Apr 9, 2015, 4:21:40 PM4/9/15 to What style name do you provide? --Steve Simon Björkunread, Apr 9, 2015, 11:00:35 PM4/9/15 to
Steve Cannyunread, 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örkunread, Apr 10, 2015, 7:24:45 AM4/10/15 to Thanks Steve, that removed the error. Great :). Any suggestion on the original post? Steve Cannyunread, 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 |