How to do it...

  1. Import the python-docx module:
>>> import docx
  1. Create a new document:
>>> document = docx.Document()
  1. Create a paragraph that has a line break:
>>> p = document.add_paragraph('This is the start of the paragraph')>>> run = p.add_run()>>> run.add_break(docx.text.run.WD_BREAK.LINE)>>> p.add_run('And now this in a different line')>>> p.add_run(". Even if it's on the same paragraph.")
  1. Create a page break and write a paragraph:
>>> document.add_page_break()>>> document.add_paragraph('This appears in a new page')
  1. Create a new section, which will be on landscape pages:
>>> section = document.add_section( docx.enum.section.WD_SECTION.NEW_PAGE)>>> section.orientation = docx.enum.section.WD_ORIENT.LANDSCAPE>>> section.page_height, ...

Get Python Automation Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.