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 
 or 
. Any other thoughts, or is this framework too limited for something like that?
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:
I get VBA of:
If I create a Word document that looks like this (pressing enter after each word):
The body of that document looks like this in the
documents.xmlfile: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:
Or:
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.pyfile of Python Docx, something like this might be the way to go (disclaimer: not tested):