O'Reilly logo

Python Automation Cookbook by Jaime Buelta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required