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
158
votes
11 answers

When import docx in python3.3 I have error ImportError: No module named 'exceptions'

when I import docx I have this error: File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/docx-0.2.4-py3.3.egg/docx.py", line 30, in from exceptions import PendingDeprecationWarning ImportError: No…
user3472559
  • 1,691
  • 2
  • 10
  • 3
84
votes
6 answers

How to extract text from an existing docx file using python-docx

I'm trying to use python-docx module (pip install python-docx) but it seems to be very confusing as in github repo test sample they are using opendocx function but in readthedocs they are using Document class. Even though they are only showing how…
Nancy
  • 997
  • 1
  • 8
  • 12
60
votes
11 answers

How to use python-docx to replace text in a Word document and save

The oodocx module mentioned in the same page refers the user to an /examples folder that does not seem to be there. I have read the documentation of python-docx 0.7.2, plus everything I could find in Stackoverflow on the subject, so please believe…
user2738815
  • 1,196
  • 3
  • 12
  • 19
36
votes
6 answers

Python-docx, how to set cell width in tables?

How to set cell width in tables?, so far I got: from docx import Document from docx.shared import Cm, Inches document = Document() table = document.add_table(rows=2, cols=2) table.style = 'TableGrid' #single lines in all cells table.autofit =…
girdeux
  • 585
  • 1
  • 5
  • 11
29
votes
6 answers

Converting docx to pdf with pure python (on linux, without libreoffice)

I'm dealing with a problem trying to develop a web-app, part of which converts uploaded docx files to pdf files (after some processing). With python-docx and other methods, I do not require a windows machine with word installed, or even libreoffice…
Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62
24
votes
2 answers

Is there any way to read .docx file include auto numbering using python-docx

Problem statement: Extract sections from .docx file including autonumbering. I tried python-docx to extract text from .docx file but it excludes the autonumbering. from docx import Document document = Document("wadali.docx") def…
wadali
  • 2,221
  • 1
  • 20
  • 38
24
votes
6 answers

Add an image in a specific position in the document (.docx)?

I use Python-docx to generate Microsoft Word document.The user want that when he write for eg: "Good Morning every body,This is my %(profile_img)s do you like it?" in a HTML field, i create a word document and i recuper the picture of the user from…
Kais Dkhili
  • 399
  • 1
  • 5
  • 18
21
votes
7 answers

combine word document using python docx

I have few word files that each have specific content. I would like for a snippet that show me or help me to figure out how to combine the word files into one file, while using Python docx library. For example in pywin32 library I did the…
omri_saadon
  • 10,193
  • 7
  • 33
  • 58
20
votes
3 answers

How to setup cell borders with python-docx

I need to setup cells borders in table with python-docx, but can't find how to. Please help.
Valentin
  • 201
  • 1
  • 2
  • 3
20
votes
1 answer

Parsing of table from .docx file

I want to parse a table from a .docx file using Python and python-docx into some useful data structure. The .docx file contains only a single table in my case. I've uploaded it so you can have a look. Here's a screenshot:
Sreedhar
  • 367
  • 1
  • 3
  • 8
19
votes
2 answers

Modify docx page margins with python-docx

I need to quickly change the margins of many docx documents. I checked python-docx and I do not find a way to access/modify the page layout (in particular the margins) properties. Is there a way?
XAnguera
  • 1,157
  • 1
  • 11
  • 25
17
votes
3 answers

Write text in particular font color in MS word using python-docx

I am trying to write text in an MS Word file using python library python-docx. I have gone through the documentation of python-docx's font color on this link and applied the same in my code, but am unsuccessful so far. Here is my code: from docx…
troy_achilies
  • 592
  • 1
  • 7
  • 15
17
votes
8 answers

Python docx Replace string in paragraph while keeping style

I need help replacing a string in a word document while keeping the formatting of the entire document. I'm using python-docx, after reading the documentation, it works with entire paragraphs, so I loose formatting like words that are in bold or…
Alo
  • 974
  • 2
  • 8
  • 24
17
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
  • 846
  • 1
  • 9
  • 24
16
votes
5 answers

Bullet Lists in python-docx

I am trying to get this to work in python-docx: A bullet list I can get using this: from docx import Document doc = Document() p = doc.add_paragraph() p.style = 'List Bullet' r = p.add_run() r.add_text("Item 1") # Something's gotta come here to…
Vizag
  • 743
  • 1
  • 7
  • 30
1
2 3
93 94