Questions tagged [python-docx]

A python library to create, read and write Microsoft Office Word 2007 docx files.

The docx module creates, reads and writes Microsoft Office Word 2007 docx files.

##Including the following features:

###Creation:

  • Paragraphs
  • Bullets
  • Numbered lists
  • Document properties (author, company, etc)
  • Multiple levels of headings
  • Tables
  • Section and page breaks
  • Images

###Modification:

  • Search and replace
  • Extract plain text of document
  • Add and delete items anywhere within the document
  • Change document properties
  • Run xpath queries against particular locations in the document - useful for retrieving data from user-completed templates.

For detailed information and examples, visit the python-docx documentation.

Available from pypi.

See also the official GitHub homepage.

1408 questions
10
votes
5 answers

Header and footer creation in versions before 0.8.8

Is there a workaround for adding headers and footers in a Microsoft Word (docx) file? These are not implemented in versions of python-docx prior to 0.8.8. More specifically, I would like to add: Page number to footers Some random text to headers…
ReKx
  • 996
  • 2
  • 10
  • 23
10
votes
1 answer

Page break via python-docx in MS Word docx file appears only at the end

I am trying to add a page break to the middle of a document using the docx-python library. It would appear that when adding a pagebreak, the page break is added to the end of the document. Is there a method to add a page break to a specific…
Robert Bailey
  • 115
  • 1
  • 3
  • 10
10
votes
6 answers

Possible to insert row at specific position with python-docx?

I'd like to insert a couple of rows in the middle of the table using python-docx. Is there any way to do it? I've tried to use a similar to inserting pictures approach but it didn't work. If not, I'd appreciate any hint on which module is a better…
T0f
  • 143
  • 1
  • 1
  • 10
10
votes
1 answer

How can I set the language in text with python-docx

I create word files using the python-docx library. I want to be able to set different parts of the document to different languages. How can the language be set with python-docx? Preferrably, I would like to do it at the run-level, since I need…
NiklasR
  • 473
  • 6
  • 19
10
votes
1 answer

Creating a table in python docx and bolding text

doc=Document() table = doc.add_table(rows = 13, cols = 5) table.style = 'Table Grid' row = table.rows[0] row.cells[0].text = ('text').bold I am trying to create a table and bold the text but cannot get the syntax right
KerryLee
  • 373
  • 2
  • 5
  • 13
10
votes
3 answers

Python Docx - Sections - Page Orientation

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 =…
Jesse
  • 277
  • 2
  • 3
  • 13
10
votes
3 answers

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…
user1427661
  • 11,158
  • 28
  • 90
  • 132
9
votes
2 answers

Adding an hyperlink in MSWord by using python-docx

I am trying to add an hyperlink in a MS Word document using docx module for Python. I searched everywhere (official doc, StackOverflow, Google) but found nothing. I would like to do something like: from docx import Document document = Document() …
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
9
votes
4 answers

Extract image position from .docx file using python-docx

I'm trying to get the image index from the .docx file using python-docx library. I'm able to extract the name of the image, image height and width. But not the index where it is in the word file import docx doc = docx.Document(filename) for s in…
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
9
votes
1 answer

How do I apply both bold and italics in python-docx?

I'm working on making a dictionary. I'm using python-docx to put it into MS Word. I can easily make it bold, or italics, but can't seem to figure out how to do both. Here's the basics: import docx word = 'Dictionary' doc = docx.Document() p =…
mightyoscar
  • 133
  • 1
  • 1
  • 10
9
votes
3 answers

Making cells bold in a table using python-docx

The code snippet below basically creates a table with the required number of rows and columns in a new word document i.e 2 columns and 14 rows. It then adds the content to the rows and columns accordingly. from docx import…
tg110
  • 401
  • 1
  • 3
  • 15
9
votes
5 answers

python-docx insertion point

I am not sure if I've been missing anything obvious, but I have not found anything documented about how one would go to insert Word elements (tables, for example) at some specific place in a document? I am loading an existing MS Word .docx document…
Apteryx
  • 5,822
  • 3
  • 16
  • 18
8
votes
3 answers

Getting the List Numbers of List Items in docx file using Python-Docx

When I am accessing paragraph text it does not include the numbering in a list. Current code: document = Document("C:/Foo.docx") for p in document.paragraphs: print(p.text) List in docx file: I am expecting: (1) The naturalization of both…
Frederick
  • 117
  • 2
  • 9
8
votes
1 answer

What is Run-level content in python-docx?

I am a bit confused about the concept of 'Run-level content' in python-docx.. I get that if I wanna check whether a paragraph is in bold or not, I need to check the run.bold, but what exactly is it? The official definition is: A run is the object…
Phillysteak
  • 95
  • 1
  • 7
8
votes
1 answer

jinja + form + unicode control characters + xml/docx integration

I am creating word documents based on a users input in a form. However, when the user inputs a unicode control character, and trying to make a word file out of this using the python-docx package, this error occurs: File "src\lxml\apihelpers.pxi",…
Joost
  • 3,609
  • 2
  • 12
  • 29
1 2
3
93 94