Install python-docx on Linux
Installing python-docx on Linux
I. Introduction
When using Python for office automation, document processing, and other tasks, we often use Python. python-docx is a Python library for creating and updating Microsoft Word (.docx) files. It provides rich functionality, such as inserting text, adding tables, and setting styles. This article will explain how to install python-docx on Linux.
2. Pre-Installation Preparations
Before installing python-docx, ensure the following packages are installed on your system:
- Python: Ensure Python is installed and that Python commands can be executed from the command line. You can check your Python version with the following command:
python --version
- pip: pip is Python’s package management tool, used to install third-party libraries. You can check your pip version with the following command:
pip --version
If the above packages are not installed, you can install them using your system’s package manager. The specific instructions vary depending on your Linux distribution.
3. Installing python-docx
Using the pip command, we can easily install python-docx. Please follow the steps below to install it:
- Open a terminal and enter the command line interface.
-
Run the following command to install python-docx:
pip install python-docx
After executing the command, pip will automatically download and install python-docx and its dependencies. This may take a while, depending on your network connection speed.
- After the installation is complete, check the version of python-docx with the following command:
python -c "import docx; print(docx.__version__)"
If the python-docx version information is output, the installation was successful.
IV. Using python-docx
After installing python-docx, we can begin using it to create and edit Word documents. Below are some basic python-docx usage examples:
1. Creating a New Document
from docx import Document
# Create a new document
doc = Document()
# Add a heading
doc.add_heading('Hello World!', level=1)
# Save the document
doc.save('hello.docx')
from docx import Document
# Create a new document
doc = Document()
# Add a heading
doc.add_heading('Hello World!', level=1)
# Save the document
doc.save('hello.docx')
Running the above code will generate a Word document named “hello.docx” with a first-level heading of “Hello World!”
2. Adding Text
from docx import Document
# Open an existing document
doc = Document('hello.docx')
# Add a new paragraph to the end of the document
doc.add_paragraph('This is a new paragraph.')
# Save the document
doc.save('hello.docx')
After running the above code, a new paragraph, “This is a new paragraph.”, will be added to the end of the document “hello.docx.”
3. Add a table
from docx import Document
# Open an existing document
doc = Document('hello.docx')
# Add a 3x3 table
table = doc.add_table(rows=3, cols=3)
# Fill in the table content
for i in range(3):
for j in range(3):
cell = table.cell(i, j)
cell.text = f'Row {i+1}, Col {j+1}'
# Save the document
doc.save('hello.docx')
After running the above code, a 3×3 table will be added to the “hello.docx” document and filled in with the table content.
4. Set style
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
# Open an existing document
doc = Document('hello.docx')
# Get the first paragraph and set the style
paragraph = doc.paragraphs[0]
paragraph.style = 'Title'
paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
paragraph.runs[0].font.size = Pt(20)
# Save the document
doc.save('hello.docx')
After running the above code, the style of the first paragraph of the “hello.docx” document will be set to “Title”, centered, and the font size will be set to 20 points.
V. Summary
This article describes how to install python-docx on Linux and how to use it to create and modify Word documents. python-docx provides a wealth of features that can meet your daily office automation and document processing needs.