How to change page size to A4 in python-docx

python-docx page layout
python-docx page numbers
python-docx page break
python-docx delete section
python-docx sections
python docx block
python-docx examples
python-docx font size

I try out creating Word documents with python-docx. The created file is in letter dimensions 8.5 x 11 inches. But in Germany the standard format is A4 8.27 x 11.69 inches.

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

I don't find any information about this topic in the documentation.

It appears that a Document is made of several Sections with page_height and page_width attributes.

To set the dimensions of the first section to A4, you could try (untested):

section = document.sections[0]
section.page_height = Mm(297)
section.page_width = Mm(210)

Note that A4 is defined in millimeters.

Working with Sections — python-docx 0.8.10 documentation, But when you do need to change the page layout, you'll need to understand sections Three properties on Section describe page dimensions and orientation. 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.

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)

feature: section page size � Issue #15 � python-openxml/python-docx , Got a question, does docx support page orientation, I think this feature ( landscape & portrait) is page orientation change mid-document #48. Files for python-docx, version 0.8.10; Filename, size File type Python version Upload date Hashes; Filename, size python-docx-0.8.10.tar.gz (5.5 MB) File type Source Python version None Upload date Jan 9, 2019 Hashes View

I believe you want this, from the documentation.

Three properties on Section describe page dimensions and orientation. Together these can be used, for example, to change the orientation of a section from portrait to landscape:

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

How do I change the paper size of my document to A4?, You can resize a word document to standard A4 size using these instructions. You may have to do this for your job to print on our public printers: Click the Page � The above python-docx code runs smoothly, but it still does not meet my requirement. The default font size of a paragraph is 11 point, but I'd like change it to a more small font size, for I want to put many words in one page, and 9 points is very good for me, how can I change the default font size in creating document by python-docx?

Python-docx, how to set cell width in tables? - ms-word - iOS, Excel document is designed for A4 papersize. That means the width grows and the last column moves to the next page. How can I influence the paper size and� How to change dendrogram labels in r; add link to change page using raw_id_fields in django admin; How to change the title on a WordPress page? Wordpress page wp-admin/edit blank for almost all functions

Re: Changing document orientation, Re: Changing document orientation Posted in group: python-docx You set it's page layout to Landscape and while you're at it you add a few styles you'll need. Then you section.page_width = Mm(297) # for A4 paper. page break; column break; section break (new page, even page, odd page) In addition, a page break can be forced by formatting a paragraph with the “page break before” setting. This analysis is limited to line, page, and column breaks. A section break is implemented using a completely different set of elements and is covered separately.

Python Examples of reportlab.platypus.SimpleDocTemplate, This page shows Python examples of reportlab.platypus. 0 self.doc = doc = SimpleDocTemplate(output, pagesize=A4, leftMargin=2.2*cm, rightMargin=2.2* cm,� Time for another iOS question. Just wondering if it is possible programmatically to change the radius of the slider's control button. I want to make the control bigger or smaller to represent the brush size that it is selecting.

Comments
  • thanks a lot!. I wasted couple of hours trying to find out how to set page margins