Reputation: 4686
I am using the python-docx module to make lots and lots of documents generated on demand from data. There are several parts when I insert figures with the conventional document.add_picture command like so:
document.add_picture(img_after_crop, width=Inches(width))
The problem I'm having is that many of the pictures are charts that finish a section so that after the chart I want to move to a new page, but the images are of different sizes so that sometimes they have already filled the page. Thus if I put a page break after the image I will sometimes get a nice figure then a new page of the next section and other times (if the image fills the page) the flow has already moved on to the next page and a page break will leave an empty page. I'm looking for a way to prevent this. If there is a good way to get current page number, I can see how to fix the problem. Would moving to using "sections" fix this (for example do sections automatically start a new page but not advance the page if we're already on a blank page)? Detecting page advancement in a document being worked on would be ideal, but I'm open to any solution...
SOLUTION
The correct answer in short form is below, but it took me some trial and error to figure out the right way to use it so for the record here is a minimum code snippet on how to use page_break_before to make a heading or paragraph jump to the next page:
from docx import Document
document = Document()
document.add_heading('Document title', 0)
para = document.add_heading("This heading goes at the top of a new page", level=1)
para.paragraph_format.page_break_before=True
document.add_paragraph('Just some text goes here. This is a normal paragraph')
document.save('Doctest.docx')
This python will generate a test word document where the title is on the first page then the heading jumps to the next page and the following paragraph just goes beneath it. The key trick is the paragraph_format access and that lets you also use alignment settings, keep_together settings, and so on from the paragraph_format options. I needed to cobble it together from a couple examples to figure out where exactly to set the value so I figured I'd post it for posterity.
Upvotes: 3
Views: 2522
Reputation: 28893
Try setting the paragraph formatting of the paragraph immediately after the image to .page_break_before = True
. The Word rendering engine is intelligent enough to not add an extra page break if the paragraph happens to occur first on a new page.
Upvotes: 4