Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Docx Carriage Return

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?

like image 576
user1427661 Avatar asked Jan 14 '13 22:01

user1427661


People also ask

How do you add a line break in docx python?

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

How do you insert a page break in python?

You can also add page breaks using this module. To add a page break in a word document you can use add_page_break() method. This method adds a hard page break in the document and creates a new paragraph object.

How do I read a docx file in python?

Reading Word Documents docx file in Python, call docx. Document() , and pass the filename demo. docx. This will return a Document object, which has a paragraphs attribute that is a list of Paragraph objects.

How do I use docx2txt in python?

You can use python-docx2txt library to read text from Microsoft Word documents. It is an improvement over python-docx library as it can, in addition, extract text from links, headers and footers. It can even extract images. You can install it by running: pip install docx2txt .


2 Answers

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)
like image 177
nick_w Avatar answered Sep 30 '22 03:09

nick_w


You can achieve your carriage return using python-docx by calling add_break() on your run. For example:

doc = Document()
p = doc.add_paragraph()
run = p.add_run()
run.add_break()

python-docx reference

like image 31
chirinosky Avatar answered Sep 30 '22 05:09

chirinosky