One Landscape Table Page in Word, Without Rotating the Rest
After constructing all sections, re-fetch the target section by its index to avoid mistakenly treating the last section object saved before adding new subsectio
Rotating one wide table to landscape in Word, while the rest of the document stays portrait, is a common automation task, and it often goes wrong. This guide is for developers and document teams who use python-docx to build or process Word files. It explains why direct property changes fail and how section breaks solve the problem.
You will see the reason behind the failure, a synthetic example you can run, and a checklist for deciding when code is enough and when a person should review the file. Keep the original copies of any important documents and verify the output against the checklist before you rely on it.
How do you rotate one Word page to landscape without rotating the rest? The short answer
Put the wide content in its own section, set that section's width, height and orientation together, then re-read the saved file to confirm the other sections did not change.
| Your situation | What to do | Why |
|---|---|---|
| The target page has no section breaks around it | Split the content with add_section() first, then set the orientation | Layout attributes belong to a section, so the content must sit in its own section |
| You set LANDSCAPE but kept the portrait width and height | Swap the width and height, and check that page_width >= page_height | Otherwise Word shows the wrong paper size and content is cropped |
| The table is still cropped after rotating | Check the section's left and right margins and the table width against the page size | The code cannot fix margins or tables that are too wide for the page |
| The header or footer is misaligned | Check the header and footer distance separately | Changing section orientation does not change the header link status |
Why rotating a table page goes wrong
Many developers try to change paragraph or table properties to rotate one page. This usually fails in one of two ways. Either the whole document turns landscape, or the table rotates and its content overflows and gets cut off.
The root cause is a mix-up between page breaks and section breaks. A page break only controls where a line ends and carries no layout attributes. Page orientation, paper size and margins are all tied to the section container. To get a landscape page in the middle of a portrait document, you must insert section breaks before and after the target content, then adjust the properties of that section.
Page breaks and section breaks compared
| Break type | Carries layout attributes? | Notes |
|---|---|---|
| Page break | No | Controls line breaks only |
| Section break (next page) | Yes: orientation, paper size, margins | Starts the new section on a new page |
| Continuous section break | Yes, as a section | Does not start on a new page, so the layout can look cluttered |
The section break carries the page layout
In the OpenXML standard, the <w:sectPr> element defines the page layout. A document can contain several sections, and each one has its own width, height, orientation and margins.
When you call document.add_section(), python-docx creates a new <w:sectPr> at the end of the current document. Any content you add afterwards belongs to that new section. Trying to change orientation through paragraph styles or table properties will not work, because those objects have no fields for layout information.
The working logic is:
- Find the content that needs to be rotated.
- Make sure it sits inside its own section.
- Swap the section's width and height values.
- Set the orientation to LANDSCAPE.
Why swapping the dimensions matters
Setting the orientation without swapping the dimensions is a frequent beginner mistake. Word then shows an incorrect paper size when the file opens, and content gets cropped when you print or export to PDF.
Correct rotation and common boundary errors
The following synthetic examples show the expected behaviour and how to handle errors. They are not real business data. If you need to process external files, load them with Document('path/to/file.docx') and make sure each file is unencrypted and structurally intact.
Correct input
The document has three sections. The first is portrait, the second holds the wide table, and the third returns to portrait.
- The second section's
orientationis LANDSCAPE. - The second section's
page_widthis greater than itspage_height. - The first and third sections stay PORTRAIT.
Error and boundary inputs
- One section only. The document has never been split into sections. If you change
sections[0]directly, the entire document turns landscape. Check the number of sections first. If the requirement is a partial rotation, calladd_section()to split the content, then change only the new section. - Width and height not swapped. If you set LANDSCAPE but keep the portrait dimensions, the code should enforce
page_width >= page_heightas a precondition.
python-docx code for a local landscape section
The example below builds a three-section document in memory and safely sets the middle section to landscape. It uses the WD_ORIENT enumeration, which is available from python-docx 0.8.11 onwards. Run it in your locked project environment, and make sure the library version you use supports this enumeration.
The example contains no file reading or writing. All objects are built in memory. To test with a real document, replace the in-memory setup with your own file path.
from docx import Document
from docx.enum.section import WD_ORIENT, WD_SECTION
from io import BytesIO
doc = Document()
doc.add_paragraph("Portrait intro")
doc.add_section(WD_SECTION.NEW_PAGE)
doc.add_paragraph("Landscape middle")
doc.add_section(WD_SECTION.NEW_PAGE)
doc.add_paragraph("Portrait end")
middle = doc.sections[1]
width, height = middle.page_width, middle.page_height
middle.orientation = WD_ORIENT.LANDSCAPE
middle.page_width, middle.page_height = max(width, height), min(width, height)
out = BytesIO()
doc.save(out)
out.seek(0)
restored = Document(out)
assert len(restored.sections) == 3
assert restored.sections[1].orientation == WD_ORIENT.LANDSCAPE
assert restored.sections[1].page_width > restored.sections[1].page_height
assert restored.sections[0].orientation == WD_ORIENT.PORTRAIT
assert restored.sections[2].orientation == WD_ORIENT.PORTRAIT
Acceptance checklist and manual review boundary
Code handles well-structured documents well, but real files often contain surprises. Use the table below to check results and to decide when to stop automating.
| Observation | Processing decision | Does the code demo apply? |
|---|---|---|
| The target page has no section breaks before or after it | Split with add_section() first, then set the direction | Yes |
| The page is still cropped after setting landscape | Check the section's left and right margins, and whether the table is wider than the page | No. Margins or the table need adjusting. |
| The document has a continuous section break and the orientation looks wrong | Continuous section breaks do not start a new page and can cause visual clutter. Switch to a next-page section break. | Partly. You must decide the section type yourself. |
The section count is 0, or indexing raises an IndexError | Check with len(sections) first, or use a try-except fallback | Yes |
| Headers and footers are misaligned after the change | Section orientation changes do not affect header link status, so check the header and footer distance separately | No. This is a separate issue. |
The code demo does not cover some files, so repair or convert these first:
- Encrypted documents
- Documents with corrupted XML structure
- Templates that use macros to generate sections dynamically
The demo only checks the API call logic. Confirm the final result visually by exporting to Word or PDF.
Reference and next step
Using this checklist for document translation
Translated Word files often mix content problems with formatting problems. Keep checks for styles, headers, tables and other elements in a separate delivery checklist, so reviewers know where to look for each kind of issue.
OmniDoc translates PDF, Word, PowerPoint and Excel files: scanned pages are recognized first, table cells are translated one by one so tables keep their rows and columns, and a PDF comes back as both a bilingual and a translation-only file. Paid translation adds a glossary and context translation. If your team wants to start from real files, it is worth evaluating with your own samples.
📊 At a glance
| Break type | Carries layout attributes? | Notes |
|---|---|---|
| Page break | No | Controls line breaks only |
| Section break (next page) | Yes: orientation, paper size, margins | Starts the new section on a new page |
| Continuous section break | Yes, as a section | Does not start on a new page, so the layout can look cluttered |
🎯 Which approach fits you?
If you need one wide table on a landscape page
Put the table in its own section and set that section's width, height and orientation together.
If your target page has no section breaks around it
Split the content with add_section() before you change anything.
If your document uses continuous section breaks and the layout looks wrong
Switch to a next-page section break, and decide the section type yourself.
❓ FAQ
1. Why does setting landscape on one table not work?
Orientation is a section-level property, and paragraph or table properties have no field for it. You need to put the content in its own section.
2. Why is my landscape page cut off when I print or export to PDF?
You probably set LANDSCAPE without swapping the width and height. Swap them so that page_width is greater than or equal to page_height.
3. Does a page break change the page orientation?
No. A page break only controls line breaks and carries no layout attributes.
4. Can this code handle encrypted or macro-generated templates?
No. Repair or convert those files first, then check the output visually in Word or PDF.
📝 Summary
Rotating one table page in Word depends on sections, not on paragraph or table properties. Split the content into its own section, swap the width and height, and check the rest of the document afterwards. Keep the original copies and verify each result against the checklist.
Try it now: upload a document and get it translated with the layout intact


