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
6
votes
2 answers

Removing Paragraph From Cell In Python-Docx

I am attempting to create a table with a two row header that uses a simple template format for all of the styling. The two row header is required because I have headers that are the same under two primary categories. It appears that the only way to…
carruthd
  • 341
  • 1
  • 3
  • 8
6
votes
2 answers

libxml2 ImportError when importing etree from lxml

I am trying to run python-docx in Enthought Canopy. I'm running Ubuntu 14.04 on VMWare Worstation 10. Whenever I try to import docx or import etree from lxml, I get an error that seems to be related to the libxml2 library: ImportError:…
ScottYoung
  • 115
  • 1
  • 2
  • 5
6
votes
3 answers

Parse .docx in python 3

I am currently writing a python 3 program that parses through certain docx files and extracts the text and images from them. I have been trying to use docx but it will not import into my program. I have installed lxml, Pillow, and python-docx yet it…
thehoule64
  • 1,761
  • 5
  • 15
  • 22
5
votes
1 answer

Is there an attribute 'fit-to-page' in add_picture() using python docx

I have added a picture in a doc by using python docx. It looks good as long as it's small. But the picture goes next page or it's displayed half of it if the size is too big. How to make my picture 'fit-to-page'. I dont want to give any constants…
Solomon Raja
  • 103
  • 6
5
votes
1 answer

Add image caption using python-docx

Im using python-docx to generate a Microsoft Word document that contains a table with images. The following code block shows the for-loop adding the images to the table: row_num = 2 img_cnt = 0 for i, var in…
JCoordes
  • 147
  • 4
  • 10
5
votes
1 answer

Python-docx: Changing line spacing for one table changes it in all tables

I am relatively new to python-docx. I am trying to change the line spacing of a table in an existing document but it changes the line spacing of all the tables in the document. Here is a minimal, reproducible example, creating from scratch a…
SamR
  • 8,826
  • 3
  • 11
  • 33
5
votes
1 answer

add two images in same line in python-docx

I am trying to add two images in docx file. Images should be one left side one right side. After using this below code the image position is working like left and right as I want but they are not on the same line I want. One is up and others are…
Ashfaque Marfani
  • 345
  • 4
  • 22
5
votes
1 answer

All paragraphs are empty in an opened document in python-docx

I do the following: from docx import Document document = Document('text.docx') document.paragraphs[42].text And it gives me '' whatever number I enter, and for loop to find and replace a word does not work. But if I save the document with…
Alexander Ivanov
  • 195
  • 2
  • 10
5
votes
1 answer

How do I view the XML produced by the python-docx package

For the purposes of unit testing, I want to check that the XML produced for a Word paragraph is what I expect when I parse an HTML paragraph. How do I extract the XML itself instead of writing to a file, unzipping the file, and re-reading the…
xnx
  • 24,509
  • 11
  • 70
  • 109
5
votes
4 answers

Unable to install python-docx (MacOS)

I am trying to install docx package. but getting the following ImportError: ImportError: cannot import name Document So as suggested here, I tried : pip install python-docx but getting the following error (python version: 2.7.15) .. .. .. …
Aditya Gupta
  • 157
  • 1
  • 8
5
votes
2 answers

How to update table of contents in docx-file with python on linux?

I've got a problem with updating table of contents in docx-file, generated by python-docx on Linux. Generally, it is not difficult to create TOC (Thanks for this answer https://stackoverflow.com/a/48622274/9472173 and this thread…
Yuriy Yosipiv
  • 53
  • 1
  • 7
5
votes
2 answers

How to set cell margins of tables in ms word using python docx

As far as I understand you can only change cell width of table.
Rohit Raj
  • 329
  • 4
  • 12
5
votes
0 answers

Python Docx: How to Set Column Width as "Autofit to contents"

I used this code: # open a document doc = docx.Document() # add a table to the end and create a reference variable # extra row is so we can add the header row t = doc.add_table(df.shape[0]+1, df.shape[1]) t.allow_autofit = True t.style =…
user1581390
  • 1,900
  • 5
  • 25
  • 38
5
votes
3 answers

python-docx does not add picture

I'm trying to insert a picture into a Word document using python-docx but running into errors. The code is simply: document.add_picture("test.jpg", width = Cm(2.0)) From looking at the python-docx documentation I can see that the following XML…
ptr0x01
  • 627
  • 2
  • 10
  • 25
5
votes
1 answer

python-docx, access to paragraph font-size

I want to access to a paragraph or run that has a unique font or font-size using python-docx. How i can get texts of runs or paragraphs that has specified font or font-size?
keramat
  • 4,328
  • 6
  • 25
  • 38