Python | Working with .docx module Last Updated : 07 Jul, 2018 Comments Improve Suggest changes Like Article Like Report Word documents contain formatted text wrapped within three object levels. Lowest level- Run objects, Middle level- Paragraph objects and Highest level- Document object. So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents in python using the python-docx module. 1. The first step is to install this third-party module python-docx. You can use pip "pip install python-docx" or download the tarball from here. Here's the Github repository. 2. After installation import "docx" NOT "python-docx". 3. Use "docx.Document" class to start working with the word document. Code #1: Python # import docx NOT python-docx import docx # create an instance of a word document doc = docx.Document() # add a heading of level 0 (largest heading) doc.add_heading('Heading for the document', 0) # add a paragraph and store # the object in a variable doc_para = doc.add_paragraph('Your paragraph goes here, ') # add a run i.e, style like # bold, italic, underline, etc. doc_para.add_run('hey there, bold here').bold = True doc_para.add_run(', and ') doc_para.add_run('these words are italic').italic = True # add a page break to start a new page doc.add_page_break() # add a heading of level 2 doc.add_heading('Heading level 2', 2) # pictures can also be added to our word document # width is optional doc.add_picture('path_to_picture') # now save the document to a location doc.save('path_to_document') Output: Notice the page break in the second page. Code #2: Now, to open a word document, create an instance along with passing the path to the document. Python # import the Document class # from the docx module from docx import Document # create an instance of a # word document we want to open doc = Document('path_to_the_document') # print the list of paragraphs in the document print('List of paragraph objects:->>>') print(doc.paragraphs) # print the list of the runs # in a specified paragraph print('\nList of runs objects in 1st paragraph:->>>') print(doc.paragraphs[0].runs) # print the text in a paragraph print('\nText in the 1st paragraph:->>>') print(doc.paragraphs[0].text) # for printing the complete document print('\nThe whole content of the document:->>>\n') for para in doc.paragraphs: print(para.text) Output: List of paragraph objects:->>> [<docx.text.paragraph.Paragraph object at 0x7f45b22dc128>, <docx.text.paragraph.Paragraph object at 0x7f45b22dc5c0>, <docx.text.paragraph.Paragraph object at 0x7f45b22dc0b8>, <docx.text.paragraph.Paragraph object at 0x7f45b22dc198>, <docx.text.paragraph.Paragraph object at 0x7f45b22dc0f0>] List of runs objects in 1st paragraph:->>> [<docx.text.run.Run object at 0x7f45b22dc198>] Text in the 1st paragraph:->>> Heading for the document The whole content of the document:->>> Heading for the document Your paragraph goes here, hey there, bold here, and these words are italic Heading level 2 Reference: https://python-docx.readthedocs.io/en/latest/#user-guide. Comment More infoAdvertise with us Next Article Python | Working with .docx module mohit_negi Follow Improve Article Tags : Technical Scripter Python Programming Language Python Programs Practice Tags : python Similar Reads Testing in Python using Doctest module Docstrings in Python are used not only for the description of a class or a function to provide a better understanding of the code and use but, also used for Testing purposes. Testing is a critical aspect of software development that ensures code functions as expected and guards against bugs. In Pyth 8 min read How to generate a documentation using Python? Documentation improves the readability of the code. There are many tools that help us to create documentations. One such tool is pdoc to write documentation for python projects. Installation: Run the following pip command on the terminal. pip3 install pdoc3 Now navigate (through command line) to th 2 min read How to Import Other Python Files? We have a task of how to import other Python Files. In this article, we will see how to import other Python Files. Python's modular and reusable nature is one of its strengths, allowing developers to organize their code into separate files and modules. Importing files in Python enables you to reuse 3 min read Run One Python Script From Another in Python In Python, we can run one file from another using the import statement for integrating functions or modules, exec() function for dynamic code execution, subprocess module for running a script as a separate process, or os.system() function for executing a command to run another Python file within the 5 min read Install Poetry to Manage Python Dependencies Poetry is a modern and user-friendly dependency management tool for Python. It simplifies the process of managing project dependencies, packaging, and publishing. In this article, we will see how to install poetry in Python in Windows. What is Python Poetry?Python Poetry is a modern and comprehensiv 2 min read reStructuredText | .rst file to HTML file using Python for Documentations Introduction to .rst file (reStructuredText): reStructuredText is a file format for Textual data majorly used by Python based communities to develop documentation in an easy way similar to other tools like Javadoc for Java. Most of the docs of Python-based software and libraries are written using re 2 min read Modulenotfounderror: No Module Named 'httpx' in Python Python is a versatile and powerful programming language used in various domains. When working with Python, you may encounter the "ModuleNotFoundError: No Module Named 'httpx'" error. This error occurs when the Python interpreter cannot find the required 'httpx' module in its library. In this article 2 min read Update Tox in Python Tox is a popular automation tool used for managing virtual environments and running tests. Keeping your Tox environments up-to-date is essential to ensure compatibility with the latest dependencies and to take advantage of bug fixes and new features. In this article, we will see how to update tox in 2 min read Building Desktop Applications in Python Desktop applications are crucial for various industries, from business management tools to personal productivity software. Python, known for its simplicity and versatility, is a great choice for building desktop applications. It offers several powerful GUI frameworks that make development easier and 8 min read Python Setup Let us see how to set up Python in our system. We can directly download the latest version of Python from the official website. Before setting up IDE you need to first install Python in your system, you can refer to this article first for step-by-step procedures. How to install Python on Linux?How t 3 min read Like