python-docx

Create, read, and update Microsoft Word .docx files with Python.

pipmacoslinuxwindows
Try with needOr install directly
Source

About

Create, read, and update Microsoft Word .docx files.

Examples

create a new word document from scratch$ python3 -c "from docx import Document; doc = Document(); doc.add_paragraph('Hello World'); doc.save('output.docx')"
read text from an existing word document$ python3 -c "from docx import Document; doc = Document('input.docx'); print('\n'.join([p.text for p in doc.paragraphs]))"
add formatted text and headings to a word document$ python3 -c "from docx import Document; doc = Document(); doc.add_heading('Title', 0); doc.add_paragraph('Formatted text'); doc.save('formatted.docx')"
insert images and tables into a word document$ python3 -c "from docx import Document; doc = Document(); doc.add_picture('image.png'); table = doc.add_table(rows=2, cols=2); doc.save('with_media.docx')"
extract and modify paragraphs in an existing docx file$ python3 -c "from docx import Document; doc = Document('input.docx'); doc.paragraphs[0].text = 'Updated text'; doc.save('modified.docx')"