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.

See also the official GitHub homepage.

1026 questions
11
votes
3 answers

How to change page size to A4 in python-docx

I try out creating Word documents with python-docx. The created file is in letter dimensions 8.5 x 11 inches. But in Germany the standard format is A4 8.27 x 11.69 inches. from docx import Document from docx.shared import Inches document =…
Georg Gutsche
  • 362
  • 1
  • 8
  • 17
11
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
  • 323
  • 2
  • 5
  • 13
11
votes
5 answers

Set paragraph font in python-docx

I am using python-docx 0.7.6. I can't seem to be able to figure out how to set font family and size for a certain paragraph. There is .style property but style="Times New Roman" doesn't work. Can somebody please point me to an example? Thanks.
MavWolverine
  • 730
  • 1
  • 7
  • 21
11
votes
8 answers

Text-Replace in docx and save the changed file with python-docx

I'm trying to use the python-docx module to replace a word in a file and save the new file with the caveat that the new file must have exactly the same formatting as the old file, but with the word replaced. How am I supposed to do this? The docx…
user2617248
  • 219
  • 1
  • 4
  • 9
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
  • 776
  • 1
  • 8
  • 23
9
votes
2 answers

How do you keep table rows together in python-docx?

As an example, I have a generic script that outputs the default table styles using python-docx (this code runs fine): import…
LMc
  • 5,865
  • 2
  • 12
  • 31
9
votes
2 answers

Page number python-docx

I am trying to create a program in python that can find a specific word in a .docx file and return page number that it occurred on. So far, in looking through the python-docx documentation I have been unable to find how do access the page number or…
lehast22
  • 151
  • 1
  • 1
  • 7
9
votes
2 answers

python-docx: 'package not found'

I have a doc.docx file at '/var/code/oa'. I need to read it use python-docx. I write this: from docx import Document document = Document('/var/code/oa/doc.docx') then, have error.. PackageNotFoundError: Package not found at…
dsphoebe
  • 113
  • 1
  • 1
  • 12
9
votes
2 answers

How to iterate over everything in a python-docx document?

I am using python-docx to convert a Word docx to a custom HTML equivalent. The document that I need to convert has images and tables, but I haven't been able to figure out how to access the images and the tables within a given run. Here is what I…
thebitguru
  • 634
  • 3
  • 9
  • 26
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
  • 4,279
  • 3
  • 12
  • 15
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
  • 2,971
  • 1
  • 9
  • 25
8
votes
2 answers

Python docx paragraph in textbox

Is there any way to access and manipulate text in an existing docx document in a textbox with python-docx? I tried to find a keyword in all paragraphs in a document by iteration: doc = Document('test.docx') for paragraph in doc.paragraphs: if…
Stefan
  • 353
  • 2
  • 13
8
votes
1 answer

python-docx style_id error while creating a word document

I'm following the tutorial available on python-docx site to create a MS-Word document but I'm getting an error: M:\Sites>python word.py C:\Program Files\IBM\SPSS\Statistics\22\Python\lib\site-packages\docx\styles\sty les.py:54: UserWarning: style…
Papouche Guinslyzinho
  • 4,477
  • 8
  • 43
  • 82
8
votes
2 answers

python-docx - how to restart list lettering

I'm automating the process of creating a Word document with the python-docx module. In particular, I'm creating a multiple choice test where the questions are numbered 1., 2., 3., ... and under each question there are 4 answers that should be…
brad14
  • 315
  • 1
  • 4
  • 9
8
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
  • 9,028
  • 21
  • 81
  • 121
1
2
3
68 69