使用docx库,可以执行各种任务
- 创建新文档:可以使用库从头开始或基于模板生成新的Word文档。这对于自动生成报告、信函和其他类型的文档非常有用。
- 修改现有文档:可以打开现有的Word文档,并使用库修改其内容、格式、样式等。这对于自动更新遵循特定结构的文档特别方便。
- 添加内容:可以使用库向文档添加段落、标题、表格、图像和其他元素。这有助于用数据动态填充文档。
- 格式化:该库允许将各种格式化选项应用于文档中的文本和元素,例如更改字体、颜色、对齐方式等。
- 提取信息:还可以从现有Word文档中提取文本、图像、表格和其他内容,以便进一步分析
Docx functions
1. 文档创建和保存
- Document(): 创建一个新的word文档
- Document.save(‘filename.docx’):保存一个document 称为文件(*.docx)
2. Paragraphs and Text (段落和文本)
- add_paragraph(‘text’): 添加具有指定文本(text)的新段落(Paragraphs)。
- paragraph.text:获取或设置段落的文本内容。
3. Headings (标题,可以设置几级标题)
- add_heading(‘text’, level=n): 添加具有指定文本和级别的标题 (1 to 9).
4. Styles and Formatting (样式与格式)
- paragraph.style = ‘StyleName’: 应用特定的段落样式
- run = paragraph.add_run(‘text’): 添加一段具有特定格式的文本
- run.bold, run.italic, etc.: 对管路(run)应用格式设置
5. Tables (表格操作)
- add_table(rows, cols): 添加具有指定行数和列数的表
- table.cell(row, col): 获取表中的特定单元格(cell)
- cell.text:获取或设置单元格的文本内容
- table.rows, table.columns:访问表的行和列
6. Images(图片操作)
- document.add_picture(‘image_path’): 向文档中添加图像
- run.add_picture(‘image_path’): 将图像添加到特定管道(run)中, 比如简历照片位置固定的
7. Document Properties (文档属性)
- document.core_properties.title: 设置文档的标题
- document.core_properties.author: 设置文档的作者
- document.core_properties.keywords: 设置文档的关键词
8. Sections and Page Setup (分区和页面设置)
- section = document.sections[0]: 获取文档的第一部分( Get the first section of the document)
- section.page_width, section.page_height: 设置页面尺寸(Set page dimensions)
9. Lists (列表)
就是markdown中的list,比如下面的这两个就是无序的,大标题1,2,3…就是有序的
- add_paragraph(‘text’, style=’ListBullet’):创建无序列表( Create a bulleted list)
- add_paragraph(‘text’, style=’ListNumber’): 创建有序列表(Create a numbered list.)
10. Hyperlinks (超链接)
- run.add_hyperlink(‘url’, ‘text’): 给当前管道(run)内的特定文本(text)添加超链接(Add a hyperlink to a run)
11. Document Modification (文件修改)
- document.paragraphs: 访问文档中的所有段落(Access all paragraphs in the document)
- document.tables: 访问文档中的所有表格(Access all tables in the document)
- document.styles: 访问和操作文档样式(Access and manipulate document styles)
12. Document Reading(文档读取)
- Document(‘filename.docx’): 读取一个存在的word文件
- document.paragraphs[0].text: 访问第一段(paragraphs)的文本(text)
小例子
1. Installation (安装)
pip install python-docx
2. 创建一个新的word文档
创建一个包含文本、标题、表格、图像和格式的文档
- Create a new document.(创建一个新的document 对象)
- Add a title with centered alignment.(添加一个标题(title)并居中对齐)
- Add a paragraph with bold and italic text.(添加带有粗体和斜体文本的段落)
- Add a heading and a bulleted list.(添加标题(heading)和项目符号列表)
- Add a table with custom column widths.(添加table,并自定义列宽)
- Add an image to the document.(添加图片)
- Save the document with the name ‘example_document.docx’.(保存文件,文件名为 example_document.docx)
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
# Create a new document
doc = Document()
# Add a title
title = doc.add_heading('Document Creation Example', level=1)
title.alignment = WD_ALIGN_PARAGRAPH.CENTER
# Add a paragraph with bold and italic text
paragraph = doc.add_paragraph('This is a sample document created using the python-docx library.')
run = paragraph.runs[0]
run.bold = True
run.italic = True
# Add a heading
doc.add_heading('Section 1: Intr