Skip to content

Add Page Break Issue #1260

@msloat450

Description

@msloat450

I am new to python-docx, so this might not be an issue but just that I don't know what I am doing. I found an example that was similar to this, but I couldn't figure it out. I was hoping someone could help.

I have been experimenting with creating a docx file that can be appended to by another word document, and so on. The process would work like this. Initially, the target document 'target.docx', when the program first ran, would just be turned to final.docx. Simple... The next time a document was added, it would combine final.docx with the target 'target.docx.' I have been using add_page_break() to separate the pages. I don't have a problem with just creating the page initially, or when I combine the files for the first time. But when I combine final.docx that has already been combined once, the page break will go after the first page of the already combined document.

Probably easier to explain this way.

  1. First page added. There is no page break added so that the page doesn't overlap another.

Page 1

  1. The second page is added to the first. The page break is done after Page 1.

Page 1

Page Break

Page 2

  1. Page 1 / Page Break / Page 2 are now one page called final.docx. It is added again to another target doc. Somehow the page break is added to the middle of the final.docx. Which at this point I am considering to be just page 1. (Or Page 1 / page break / Page 2)

Page 1

Page Break

Page Break

Page 2

Page 3.

The original program I had trouble with, so I decided to knock it down into parts to see what was going on. Keep in mind that demo is like the final.docx and demo1 is similar to the target document. When creating the original file, all I do it use docs = Document('pre.docx') and then save it as testReport1.docx.

demo = 'testReport1.docx'

demo1 = 'pre.docx'
doc = []
doc.append(Document(demo))

doc.append(Document(demo1))

doc[0].add_page_break()
doc[0].add_paragraph('')

for ep in doc[1].element.body:
doc[0].element.body.append(ep)

doc[0].save('testReport1.docx')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions