Skip to content
Advertisement

How to add a Sectional Break in word using python-docx

I have been trying to add a sectional break into my word document using python-docx. I basically want to add a sectional break before every paragraph having style = “heading 1”. I have written the following code. The code goes as follows:

1)get the total number of paragraphs

2)find the index of the paragraph with style = “heading 1”

3)add a run to the paragraph which is before the paragraph having style = “heading 1”

4)add a sectional break to the run

JavaScript

But it gives me this error.

JavaScript

How should I add the sectional break?

Advertisement

Answer

python-docx currently has no API support for inserting a section break at an arbitrary location. You can only add a new section at the end of the document, typically in the course of generating a new document from the top down to the bottom.

JavaScript

There is more on adding a section on the documentation page here:
https://python-docx.readthedocs.io/en/latest/user/sections.html

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement