How to Create a Word Document from Scratch via Python Code
Tags: programming, tutorial, python, docx
Published: 2023-10-20 07:49:46

Creating documentation through a programming language is an automated approach. You can use specialized libraries to do this. Even using code, you can also customize the format and layout of your document to suit your needs. This method can generate documents in batches, thereby improving work efficiency and reducing complicated manual operations. Below are steps to create a simple Word document using Python code.

## Tool

- Visual Studio Code
- [Spire.Doc for Python](https://www.e-iceblue.com/Introduce/doc-for-python.html)

## Installation
You can install [Spire.Doc for Python](https://www.e-iceblue.com/Introduce/doc-for-python.html) in Visual Studio Code by performing the following steps:

- First, make sure that you have [download and install Python](https://www.python.org/downloads/). 
- Open VS Code, click “Extensions”, search for “Python” and then install it.
- Click “Explorer” > “NO FOLRDER OPENED” > “Open Folder”.
- Choose a folder.
- Add a “.py” file to this folder and name it whatever you like.
- Click “Terminal” > “New Terminal”.
- Last, input the following commands
```
pip install Spire.Doc
```
## Code Example:
This library provides a Document class that is used to represent a model of a Word document. A document must contain at least one section. Each section can contain elements such as paragraphs, tables, charts, and images. Below is a demo of the code. You can modify the code as needed to create the Word document you want.
```python
from spire.doc import * 
from spire.doc.common import *

# Create a Document object
doc = Document()

# Add a section
section = doc.AddSection()

# Set the page margins
section.PageSetup.Margins.All = 40

# Add a title
titleParagraph = section.AddParagraph()
titleParagraph.AppendText("Test")

# Add two paragraphs
bodyParagraph_1 = section.AddParagraph()
bodyParagraph_1.AppendText("This is a test text.")

bodyParagraph_2 = section.AddParagraph()
bodyParagraph_2.AppendText("This is a test text.")

# Apply heading1 to the title
titleParagraph.ApplyStyle(BuiltinStyle.Heading1)

# Create a style for the paragraphs
style2 = ParagraphStyle(doc)
style2.Name = "paraStyle"
style2.CharacterFormat.FontName = "Arial"
style2.CharacterFormat.FontSize = 12
doc.Styles.Add(style2)
bodyParagraph_1.ApplyStyle("paraStyle")
bodyParagraph_2.ApplyStyle("paraStyle")

# Set the horizontal alignment of the paragraphs
titleParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center
bodyParagraph_1.Format.HorizontalAlignment = HorizontalAlignment.Left
bodyParagraph_2.Format.HorizontalAlignment = HorizontalAlignment.Left

# Set the after spacing
titleParagraph.Format.AfterSpacing = 10
bodyParagraph_1.Format.AfterSpacing = 10

# Save to file
doc.SaveToFile("WordDocument.docx", FileFormat.Docx2019)
```
This Word Python library not only supports processing Word documents easily, but also allows users to convert Word to other formats with high-quality, such as [PDF](https://www.e-iceblue.com/Tutorials/Python/Spire.Doc-for-Python/Program-Guide/Conversion/Python-Convert-Word-to-PDF.html), [Text](https://www.e-iceblue.com/Tutorials/Python/Spire.Doc-for-Python/Program-Guide/Conversion/Python-Convert-Text-to-Word-or-Word-to-Text.html) or [HTML](https://www.e-iceblue.com/Tutorials/Python/Spire.Doc-for-Python/Program-Guide/Conversion/Python-Convert-Word-to-HTML.html).

## Screenshot

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e56c7e6trfkz7fx7qxd0.png)