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
15
votes
10 answers

python docx set table cell background and text color

I am using python 2.7 with docx and I would like to change the background and text color of cells in my table based on condition. I could not find any usefull resources about single cell formatting Any suggestions? Edit 1 my code style_footer =…
Yebach
  • 1,661
  • 8
  • 31
  • 58
14
votes
1 answer

python -docx to extract table from word docx

I know this is a repeated question but the other answers did not work for me. I have a word file that consists of one table. I want that table as an output of my python program. I'm using python 3.6 and I have installed python -docx as well. Here is…
Aroon
  • 991
  • 2
  • 15
  • 34
14
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
  • 452
  • 4
  • 9
  • 17
12
votes
7 answers

Add page number using python-docx

I am trying to add a page number in the footer of a word doc using python-docx. So far, I haven't been able to find how to do so. This question address how to find a page number (or how you cannot). This one talks about creating a template and…
max_max_mir
  • 1,494
  • 3
  • 20
  • 36
12
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
  • 423
  • 1
  • 4
  • 14
12
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
  • 872
  • 3
  • 12
  • 30
12
votes
4 answers

Python: Create a "Table Of Contents" with python-docx/lxml

I'm trying to automate the creation of .docx files (WordML) with the help of python-docx (https://github.com/mikemaccana/python-docx). My current script creates the ToC manually with following loop: for chapter in myChapters: …
Joseph jun. Melettukunnel
  • 6,267
  • 20
  • 69
  • 90
12
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
  • 329
  • 1
  • 5
  • 9
11
votes
10 answers

docx.opc.exceptions.PackageNotFoundError: Package not found at

Can't open doxc file by path (Package not found at...) View of directory: Code: from docx import Document # some path of file: path = 'TestDir/dir2/doc22.docx' # open docx file: doc = Document(path) Have this: Traceback (most recent call…
Django Girl
  • 121
  • 1
  • 1
  • 4
11
votes
1 answer

When using Python docx how to enable spelling in output document?

I'm using the Python docx library to generate an document, that I would like to be able to open and find spelling errors in. But when I open the document none of the spelling errors flag (little red underlines) or are identified if I run a spell…
Colin Talbert
  • 454
  • 6
  • 20
11
votes
6 answers

docx center text in table cells

So I am starting to use pythons docx library. Now, I create a table with multiple rows, and only 2 columns, it looks like this: Now, I would like the text in those cells to be centered horizontally. How can I do this? I've searched through docx API…
minecraftplayer1234
  • 2,127
  • 4
  • 27
  • 57
11
votes
3 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
  • 12,577
  • 3
  • 31
  • 43
11
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
  • 181
  • 1
  • 1
  • 8
11
votes
3 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…
ooooh326
  • 133
  • 1
  • 1
  • 13
11
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
  • 345
  • 2
  • 5
  • 10
1
2
3
93 94