在日常办公和文档处理中,Word文档是我们最常用的格式之一。Python作为强大的编程语言,通过python - docx
库,能够实现对Word文档的各种自动化操作,极大地提高工作效率。本文将分享多个实用案例,带你领略Python与Word的完美结合。
1. 创建简单的Word文档
1 2 3 4 5 6 7 8 9 10 | from docx import Document def create_simple_doc(): doc = Document() doc.add_heading( '这是一个新的Word文档' , level = 1 ) doc.add_paragraph( '这是一段示例文本。' ) doc.save( 'simple_doc.docx' ) create_simple_doc() print ( "简单Word文档创建成功!" ) |
解释
此脚本使用python - docx
库创建了一个新的Word文档。首先,导入Document
类,它是操作Word文档的核心类。接着,创建一个Document
对象,使用add_heading
方法添加了一个一级标题,add_paragraph
方法添加了一段文本,最后使用save
方法保存文档。对于需要快速生成简单模板文档的场景,这个功能非常实用。
2. 在文档中插入图片
1 2 3 4 5 6 7 8 9 10 11 12 | from docx import Document from docx.shared import Inches def insert_image(): doc = Document() doc.add_heading( '插入图片示例' , level = 1 ) doc.add_paragraph( '以下是插入的图片:' ) doc.add_picture( 'example.jpg' , width = Inches( 4 )) doc.save( 'image_doc.docx' ) insert_image() print ( "图片成功插入到Word文档!" ) |
解释
该脚本展示了如何在Word文档中插入图片。通过add_picture
方法实现,其中width
参数使用Inches
类来指定图片的宽度,让图片大小符合排版需求。在制作产品介绍文档、技术报告等需要图文并茂的场景中,这个功能可以使文档更加生动直观。
3. 批量替换文档中的文本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from docx import Document def replace_text(file_path, old_text, new_text): doc = Document(file_path) for paragraph in doc.paragraphs: if old_text in paragraph.text: paragraph.text = paragraph.text.replace(old_text, new_text) for table in doc.tables: for row in table.rows: for cell in row.cells: if old_text in cell.text: cell.text = cell.text.replace(old_text, new_text) doc.save(file_path) replace_text( 'example.docx' , '旧文本' , '新文本' ) print ( "文本替换成功!" ) |
解释
此脚本可以批量替换Word文档中的文本。它遍历文档中的段落和表格中的单元格,一旦发现包含指定的旧文本,就将其替换为新文本。在处理大量相似文档,需要修改其中固定内容时,这个功能可以节省大量时间。
4. 提取文档中的文本内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from docx import Document def extract_text(file_path): doc = Document(file_path) text = "" for paragraph in doc.paragraphs: text + = paragraph.text + "n" for table in doc.tables: for row in table.rows: for cell in row.cells: text + = cell.text + "n" return text extracted_text = extract_text( 'example.docx' ) print (extracted_text) |
解释
该脚本用于提取Word文档中的所有文本内容,包括段落和表格中的文字。在需要对文档内容进行分析、统计词频等操作时,首先要获取文档中的文本,这个功能就提供了基础支持。
5. 设置文档的页面布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from docx import Document from docx.shared import Cm def set_page_layout(): doc = Document() section = doc.sections[ 0 ] section.page_width = Cm( 21 ) section.page_height = Cm( 29.7 ) section.left_margin = Cm( 2.54 ) section.right_margin = Cm( 2.54 ) section.top_margin = Cm( 2.54 ) section.bottom_margin = Cm( 2.54 ) doc.save( 'layout_doc.docx' ) set_page_layout() print ( "页面布局设置成功!" ) |
解释
这个脚本用于设置Word文档的页面布局,包括页面大小和页边距。通过section
对象来调整相关属性,使用Cm
类来指定长度单位为厘米。在排版正式文档,如论文、商务报告时,合适的页面布局可以提升文档的专业性和美观度。
6. 在文档中添加表格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | from docx import Document def add_table(): doc = Document() doc.add_heading( '示例表格' , level = 1 ) table = doc.add_table(rows = 3 , cols = 3 ) table.style = 'Table Grid' hdr_cells = table.rows[ 0 ].cells hdr_cells[ 0 ].text = '列1' hdr_cells[ 1 ].text = '列2' hdr_cells[ 2 ].text = '列3' for i in range ( 1 , 3 ): row_cells = table.rows[i].cells row_cells[ 0 ].text = f '第{i}行,第1列' row_cells[ 1 ].text = f '第{i}行,第2列' row_cells[ 2 ].text = f '第{i}行,第3列' doc.save( 'table_doc.docx' ) add_table() print ( "表格成功添加到Word文档!" ) |
解释
该脚本展示了在Word文档中创建表格并填充内容的过程。使用add_table
方法创建一个3行3列的表格,设置表格样式为Table Grid
,然后分别对表头和表格内容进行填充。在制作数据对比文档、项目进度表等场景中,添加表格可以清晰地展示数据。
7. 设置文本的字体格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | from docx import Document from docx.shared import Pt from docx.enum.text import WD_COLOR_INDEX def set_font_style(): doc = Document() p = doc.add_paragraph( '这是一段设置了字体格式的文本。' ) run = p.runs[ 0 ] run.font.name = '宋体' run.font.size = Pt( 12 ) run.font.bold = True run.font.italic = True run.font.underline = True run.font.color.rgb = RGBColor( 0 , 0 , 255 ) run.font.highlight_color = WD_COLOR_INDEX.YELLOW doc.save( 'font_style_doc.docx' ) set_font_style() print ( "字体格式设置成功!" ) |
解释
此脚本用于设置Word文档中一段文本的字体格式。通过run
对象来操作字体属性,包括字体名称、字号、加粗、倾斜、下划线、颜色和突出显示颜色等。在需要强调文档中的某些关键内容时,设置独特的字体格式可以吸引读者的注意力。
8. 为文档添加页眉页脚
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from docx import Document def add_header_footer(): doc = Document() section = doc.sections[ 0 ] header = section.header footer = section.footer header.paragraphs[ 0 ].text = '这是页眉内容' footer.paragraphs[ 0 ].text = '这是页脚内容,页码:' footer.add_page_number() doc.save( 'header_footer_doc.docx' ) add_header_footer() print ( "页眉页脚添加成功!" ) |
解释
该脚本为Word文档添加了页眉和页脚。通过section
对象获取页眉和页脚,然后在页眉和页脚的段落中添加文本,并在页脚中添加页码。在制作多页文档时,页眉页脚可以提供文档的标识信息和页码,方便阅读和管理。
9. 合并多个Word文档
1 2 3 4 5 6 7 8 9 10 11 12 13 | from docx import Document def merge_docs(doc_paths, output_path): result_doc = Document() for doc_path in doc_paths: sub_doc = Document(doc_path) for element in sub_doc.element.body: result_doc.element.body.append(element) result_doc.save(output_path) doc_paths = [ 'doc1.docx' , 'doc2.docx' ] merge_docs(doc_paths, 'merged_doc.docx' ) print ( "多个Word文档合并成功!" ) |
解释
此脚本可以将多个Word文档合并成一个。它遍历每个输入文档的内容元素,将其追加到结果文档中,最后保存合并后的文档。在整理项目文档、汇编报告等场景中,合并多个相关文档可以提高文档的整体性和可读性。
10. 从模板生成个性化文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | from docx import Document def generate_personalized_doc(template_path, data): doc = Document(template_path) for paragraph in doc.paragraphs: for key, value in data.items(): if key in paragraph.text: paragraph.text = paragraph.text.replace(key, value) for table in doc.tables: for row in table.rows: for cell in row.cells: for key, value in data.items(): if key in cell.text: cell.text = cell.text.replace(key, value) doc.save( 'personalized_doc.docx' ) data = { '姓名' : '张三' , '年龄' : '25' , '职位' : '工程师' } generate_personalized_doc( 'template.docx' , data) print ( "个性化文档生成成功!" ) |
解释
该脚本基于模板文档生成个性化文档。通过替换模板中的占位符(如姓名
、年龄
等)为实际数据,实现快速生成个性化的文档。在制作批量的合同、邀请函、简历等场景中,利用模板生成个性化文档可以大大提高工作效率。
通过以上这些案例,我们可以看到python - docx
库在处理Word文档时的强大功能和灵活性。无论是简单的文档创建,还是复杂的文档内容处理和格式设置,Python都能轻松应对。
到此这篇关于10个Python中python_docx库的实用案例分享的文章就介绍到这了,更多相关python_docx使用内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!