Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9181293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:16:39+00:00 2026-06-17T18:16:39+00:00

Python Docx is a pretty good library for generating Microsoft Word documents for something

  • 0

Python Docx is a pretty good library for generating Microsoft Word documents for something that doesn’t directly deal with all of the COM stuff. Nonetheless, I’m running into some limitations.

  • Does anyone have any idea how one would put a carriage return in a string of text?

I want a paragraph to have multiple lines without there being extra space between them. However, writing out a string that separates the lines with the usual \n is not working. Nor is using &#10 or &#13. Any other thoughts, or is this framework too limited for something like that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:16:41+00:00Added an answer on June 17, 2026 at 6:16 pm

    I’m not sure if this is possible. It looks as though Word is in fact treating presses of the enter key (I am treating this action as a sort of programmatic equivalent of “\r\n” and “\n”) as the creation of a new paragraph.


    If I record a macro in Word that consists of:

    1. Typing the text “One”
    2. Pressing the enter key

    I get VBA of:

    Selection.TypeText Text:="One"
    Selection.TypeParagraph
    

    If I create a Word document that looks like this (pressing enter after each word):

    One
    
    Two
    
    Three
    

    The body of that document looks like this in the documents.xml file:

    <w:body>
        <w:p w:rsidR="00BE37B0" w:rsidRDefault="00CF2350">
            <w:r>
                <w:t>One</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00CF2350" w:rsidRDefault="00CF2350">
            <w:r>
                <w:t>Two</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00CF2350" w:rsidRDefault="00CF2350">
            <w:r>
                <w:t>Three</w:t>
            </w:r>
        </w:p>
        <w:sectPr w:rsidR="00CF2350" w:rsidSect="001077CC">
            <w:pgSz w:w="11906" w:h="16838"/>
            <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
            <w:cols w:space="708"/>
            <w:docGrid w:linePitch="360"/>
        </w:sectPr>
    </w:body>
    

    From MSDN we can see that the <w:p> element represents a paragraph.


    I think the solution to this would be to follow the example in Python Docx:

    body.append(paragraph("Hi."))
    body.append(paragraph("My name is Alice."))
    body.append(paragraph("Let's code"))
    

    Or:

    for paragraph_text in "Hi. \nMy name is Alice.\n Let's code".split("\n"):
        body.append(paragraph(paragraph_text.strip()))
    

    Edit:

    Looking into this some more, if you press Shift + Enter in Word it adds a manual line break (not a paragraph) via appending Chr(11). In Open XML, this translates to a Break.

    Looking at the docx.py file of Python Docx, something like this might be the way to go (disclaimer: not tested):

    for text in "Hi. \nMy name is Alice.\n Let's code".split("\n"):
        run = makeelement('r')
        run.append(makeelement('t', tagtext=text))
        run.append(makeelement('br'))
        body.append(run)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want o use Python-docx library to process word files. A docx.py references lxml,
On Python 2.7, for dir in os.listdir(E:/Library/Documents/Old - Archives/Case): print dir prints out: Danny.xlsx
I am using the python library docx: http://github.com/mikemaccana/python-docx My goal is to open a
Python's inner/nested classes confuse me. Is there something that can't be accomplished without them?
I have written an application using Python 2.7 and Tkinter that edits *.docx files.
(Python) Given two numbers A and B. I need to find all nested groups
Python beginner here. I have a text file that is sorted into columns: fields
Python is so dynamic that it's not always clear what's going on in a
[Python/MySQLdb] - CentOS - Linux - VPS I have a page that parses a
Looking to develop server-side application that will process documents. The source documents are mostly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.