Reading and Writing MS Word Files in Python via Python-Docx Module
Microsoft Office套件中的MS Word实用程序是用于编写简单和复杂的文本文档的最常用工具之一。 尽管人们可以轻松地读写MS Word文档,但前提是您已安装Office软件,但通常您需要从另一个应用程序中的Word文档中读取文本。
例如,如果您正在使用Python开发自然语言处理应用程序,以MS Word文件作为输入,则需要先使用Python读取MS Word文件,然后才能处理文本。 同样,通常您需要将文本写到MS Word文档中作为输出,例如,它可以是动态生成的报告以供下载。
在本文中,您将看到如何在Python中读取和写入MS Word文件。
安装Python-Docx库
存在几个可用于在Python中读写MS Word文件的库。 但是,由于其易用性,我们将使用python-docx模块。 在终端中执行以下
1 | $ pip install python-docx |
使用Python-Docx模块读取MS Word文件
在本节中,您将看到如何通过
创建一个新的MS Word文件并将其重命名为" my_word_file.docx"。 我将文件保存在" E"目录的根目录中,尽管您可以将文件保存在任何位置。 my_word_file.docx文件应具有以下内容:
要读取上述文件,请首先导入
1 2 3 |
阅读段落
使用文件路径创建
1 2 | all_paras = doc.paragraphs len(all_paras) |
输出:
1 | 10 |
现在,我们将迭代地打印my_word_file.docx文件中的所有段落:
1 2 3 | for para in all_paras: print(para.text) print("-------") |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | ------- Introduction ------- ------- Welcome to stackabuse.com ------- The best site for learning Python and Other Programming Languages ------- Learn to program and write code in the most efficient manner ------- ------- Details ------- ------- This website contains useful programming articles for Java, Python, Spring etc. ------- |
输出显示Word文件中的所有段落。
我们甚至可以通过索引
1 2 | single_para = doc.paragraphs[4] print(single_para.text) |
输出:
1 | The best site for learning Python and Other Programming Languages |
阅读运行
单词文档中的行是具有相似属性(如相似的字体大小,字体形状和字体样式)的连续单词序列。 例如,如果您查看my_word_file.docx的第二行,其中包含文本" Welcome to stackabuse.com",此处的文本" Welcome to"为纯字体,而文本" stackabuse.com"为 黑体。 因此,文本" Welcome to"被视为一次运行,而黑体字" stackabuse.com"被视为另一次运行。
类似地,"以最有效的方式编程和编写代码"和"以最有效的方式编写代码"在段落"以最有效的方式编写程序和代码"中被视为两个不同的运行。
要获得段落中的所有运行,可以使用
让我们阅读文本中第5段(第4个索引)的所有运行:
1 2 3 | single_para = doc.paragraphs[4] for run in single_para.runs: print(run.text) |
输出:
1 2 3 4 | The best site for learning Python and Other Programming Languages |
以相同的方式,以下脚本将打印my_word_file.docx文件第6段中的所有运行:
1 2 3 | second_para = doc.paragraphs[5] for run in second_para.runs: print(run.text) |
输出:
1 2 | Learn to program and write code in the most efficient manner |
使用Python-Docx模块编写MS Word文件
在上一节中,您了解了如何使用
要写入MS Word文件,您必须使用空的构造函数创建
1 |
撰写段落
要编写段落,可以使用
下面的脚本将一个简单的段落写到一个新创建的MS Word文件中,该文件名为" my_writing_file.docx"。
1 2 | mydoc.add_paragraph("This is first paragraph of a MS Word file.") mydoc.save("E:/my_written_file.docx") |
一旦执行了上面的脚本,您应该在
让我们在my_writing_file.docx中添加另一个段落:
1 2 | mydoc.add_paragraph("This is the second paragraph of a MS Word file.") mydoc.save("E:/my_written_file.docx") |
第二段将添加到my_write_file.docx中现有内容的末尾。
写作奔跑
您也可以使用
1 2 3 | third_para = mydoc.add_paragraph("This is the third paragraph.") third_para.add_run(" this is a section at the end of third paragraph") mydoc.save("E:/my_written_file.docx") |
在上面的脚本中,我们使用
写作标题
您还可以将标题添加到MS Word文件。 为此,您需要调用
以下脚本将级别0、1和2的三个标头添加到文件my_write_file.docx中:
1 2 3 4 | mydoc.add_heading("This is level 1 heading", 0) mydoc.add_heading("This is level 2 heading", 1) mydoc.add_heading("This is level 3 heading", 2) mydoc.save("E:/my_written_file.docx") |
添加图像
要将图像添加到MS Word文件,可以使用
1 2 | mydoc.add_picture("E:/eiffel-tower.jpg", width=docx.shared.Inches(5), height=docx.shared.Inches(7)) mydoc.save("E:/my_written_file.docx") |
执行本文的"使用Python-Docx模块编写MS Word文件"部分中的所有脚本后,最终的my_write_file.docx文件应如下所示:
在输出中,您可以看到添加到MS Word文件的三个段落,以及三个标题和一个图像。
结论
本文简要概述了如何使用