10

The following code tries to use landscape orientation, but the document is created as potrait.
Can you suggest where the problem is?

from docx import Document
from docx.enum.section import WD_ORIENT

document = Document()

section = document.sections[-1]
section.orientation = WD_ORIENT.LANDSCAPE

document.add_heading('text')
document.save('demo.docx')

When I read the code back as XML

<w:document>
    <w:body>
       <w:p>
          <w:pPr>
             <w:pStyle w:val="Heading1"/>
          </w:pPr>
          <w:r>
              <w:t>TEXT</w:t>
          </w:r>
       </w:p>
       <w:sectPr w:rsidR="00FC693F" w:rsidRPr="0006063C" w:rsidSect="00034616">
           <w:pgSz w:w="12240" w:h="15840" w:orient="landscape"/>
           <w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="720" w:footer="720" w:gutter="0"/>
           <w:cols w:space="720"/>
           <w:docGrid w:linePitch="360"/>
        </w:sectPr>
    </w:body>
 </w:document>

I don't know XML well by assume the section tags should come above the TEXT tags at the top rather than the bottom????

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Jesse
  • 277
  • 2
  • 3
  • 13
  • 1
    Can you elaborate on "but not working": what do you get? – boardrider Aug 09 '15 at 20:22
  • 1
    Word document comes out in portrait – Jesse Aug 10 '15 at 07:44
  • Which Python and OS are you using? When I try to run your code as is, I get `ImportError: cannot import name Document`: > $ cat stackoverflow1.py > > from docx import Document > from docx.enum.section import WD_ORIENT > > document = Document() > > section = document.sections[-1] > section.orientation = WD_ORIENT.LANDSCAPE > > document.add_heading('text') > document.save('demo.docx') > > $ python stackoverflow1.py > Traceback (most recent call last): > File "stackoverflow1.py", line 1, in > from docx import Document > ImportError: cannot import name Document > – boardrider Aug 10 '15 at 11:14

3 Answers3

18

Whilst the page is correctly tagged as landscape, its dimensions remain the same as before and must be manually changed.

http://python-docx.readthedocs.io/en/latest/user/sections.html

Page dimensions and orientation

Three properties on Section describe page dimensions and orientation. Together these can be used, for example, to change the orientation of a section from portrait to landscape:

...

new_width, new_height = section.page_height, section.page_width section.orientation = WD_ORIENT.LANDSCAPE section.page_width = new_width section.page_height = new_height

Dragon
  • 2,017
  • 1
  • 19
  • 35
7

I have made a function that makes it easy to change from landscape orientation to portrait orientation and viceversa:

def change_orientation():
    current_section = document.sections[-1]
    new_width, new_height = current_section.page_height, current_section.page_width
    new_section = document.add_section(WD_SECTION.NEW_PAGE)
    new_section.orientation = WD_ORIENT.LANDSCAPE
    new_section.page_width = new_width
    new_section.page_height = new_height

    return new_section

Then just use it whenever you want:

change_orientation()
document.add_picture(ax1)
change_orientation()
document.add_picture(ax2)
j4n7
  • 590
  • 7
  • 9
  • Thanks, this was helpful. To make it work, I had to also add the command: From docx.enum.section import WD_ORIENT – HyperMouse Jun 05 '23 at 06:31
-3
def change_orientation():
    current_section = document.sections[-1]
    new_width, new_height = current_section.page_height, current_section.page_width
    new_section = document.add_section(WD_SECTION_START.NEW_PAGE)
    new_section.orientation = WD_ORIENTATION.LANDSCAPE
    new_section.page_width = new_width
    new_section.page_height = new_height

    return new_section

new code !