MS Word files are immensely used to create various types of documents such as invoices, reports, technical articles, etc. The document automation has facilitated the users to generate Word documents dynamically from within their web or desktop portals. Therefore, in this article, we will cover how to generate Word documents in Python without MS Office. Moreover, you will learn how to create a DOCX or DOC file and add text or other elements into it dynamically using Python.
- Python API to Create Word Documents
- Create a Word DOCX/DOC File in Python
- Load an Existing Word Document
- Insert Paragraph in a Word Document
- Add Table in a Word Document
- Add List in a Word DOCX/DOC File
- Insert Image in a Word Document
- Add Table of Contents in a Word File
TIP: Aspose provides a FREE online PowerPoint to Word Converter that allows you to create Word documents from presentations quickly.
Python API to Create Word Documents
In order to create Word documents dynamically, we will use Aspose.Word for Python. It is a powerful Python library that lets you create and manipulate MS Word documents seamlessly. You can install the library in your Python application from PyPI using the following pip command.
pip install aspose-words
Create a Word DOCX or DOC File in Python
The following are the steps to create a Word document from scratch in Python.
- Create an object of Document class.
- Create an object of DocumentBuilder class.
- Insert text into document using DocumentBuilder.write() method.
- Save the Word document using Document.save() method.
The following code sample shows how to create a Word DOCX document.
Output
Load an Existing Word Document in Python
You can also load an existing Word document by providing its path to the Document constructor. The following code sample shows how to load a Word document.
Insert a Paragraph in Word Document using Python
The Word documents contain text in the form of paragraphs. Therefore, in this section, we will demonstrate how to insert a paragraph into a Word document using Python.
- Create an object of Document class.
- Create an object of DocumentBuilder class.
- Get reference of font from the Documentbuilder object and set font.
- Get reference of the paragraph fromat from Documentbuilder object and set indentation, alignment, etc.
- Insert text into paragraph using DocumentBuilder.write() method.
- Save the Word document using Document.save() method.
The following code sample shows how to insert a paragraph in a Word document using Python.
Output
Learn more about working with paragraphs in Word documents using Python.
Add Table in a Word Document using Python
Tables are an integral part of the documents to display information in the form of rows and columns. Aspose.Words for Python makes it quite easier to work with tables. The following are the steps to add a table in a Word document using Python.
- Create an object of Document class.
- Create an object of DocumentBuilder class.
- Start a table using DocumentBuilder.start_table() method and get reference of the table in an object.
- Insert a cell using DocumentBuilder.insert_cell() method.
- Set auto fit using auto_fit(aw.tables.AutoFitBehavior.FIXED_COLUMN_WIDTHS) method.
- Set alignment of the cell.
- Insert text into cell using DocumentBuilder.write() method.
- Repeat inserting cells and text into cells as required.
- End a row when you complete inserting cells.
- End table when you have inserted all the rows.
- Save the Word document using Document.save() method.
The following code sample shows how to insert a table in a Word document using Python.
Output
Learn more about how to work with tables in Word documents using Python.
Create a List in a Word Document using Python
The following are the steps to create a list in a Word document using Python.
- Create an object of Document class.
- Create an object of DocumentBuilder class.
- Set formatting using DocumentBuilder.list_format.apply_number_default() method.
- Insert item using DocumentBuilder.writeln(“Item 1”) method.
- Insert second item using DocumentBuilder.writeln(“Item 2”) method.
- To insert items into next level of the list, call DcoumentBuilder.list_format.list_indent() method and insert items.
- Remove numbers from the list using DcoumentBuilder.list_format.remove_numbers() method.
- Save the Word document using Document.save() method.
The following code sample shows how to create a list in Word documents using Python.
Output
Read more about working with lists in Word documents using Python.
Insert Images in a Word Document using Python
While working with Word documents, you cannot ignore graphical objects such as images. So let’s have a look at how to insert images in a Word document dynamically using Python.
- Create an object of Document class.
- Create an object of DocumentBuilder class.
- Insert image using DocumentBuilder.insert_image() method and pass image file’s path as parameter.
- Save the Word document using Document.save() method.
The following code sample shows how to insert an image in a Word document using Python.
Output
Create Table of Contents in Word Documents using Python
In various cases, the Word documents contain a table of content (TOC). The TOC gives you an overview of the content of the Word document. The following steps demonstrate how to add a TOC in Word documents using Python.
- Create an object of Document class.
- Create an object of DocumentBuilder class.
- Insert table of contents using DocumentBuilder.insert_table_of_contents() method.
- Insert a page break after TOC using DocumentBuilder.insert_break(aw.BreakType.PAGE_BREAK) method.
- After adding/updating content of the document, update TOC using Document.update_fields() method.
- Save the Word document using Document.save() method.
The following code sample shows how to insert a table of contents in a Word document using Python.
Output
Read more about working with the table of contents using Python.
Read More
This article covered some basic operations of creating Word documents and inserting different elements. Aspose.Words for Python provides a bunch of other features as well that you can explore using the documentation.
Get a Free API License
You can get a temporary license in order to use Aspose.Words for Python without evaluation limitations.
Conclusion
In this article, you have learned how to create Word DOCX or DOC files in Python. Moreover, you have seen how to insert text, images, tables, lists, and table of contents in Word documents dynamically. You can try the API and share your feedback or queries via our forum.