# 导入必要的库
from docx import Document
def read_word_file(file_path):
# 打开word文档
doc = Document(file_path)
# 创建一个空字符串来存储文档内容
full_text = []
# 遍历文档中的每个段落,并将内容添加到full_text列表中
for para in doc.paragraphs:
full_text.append(para.text)
# 将列表中的所有段落内容合并为一个字符串并返回
return '\n'.join(full_text)
# 示例用法
file_path = 'example.docx' # 替换为你的Word文件路径
content = read_word_file(file_path)
print(content)
python-docx 库来读取 .docx 文件。确保你已经安装了这个库,可以使用 pip install python-docx 来安装。Document(file_path) 打开指定路径的 Word 文档。doc.paragraphs 获取文档中的所有段落,并将每个段落的内容添加到一个列表中。上一篇:python列表操作
下一篇:python emumerate
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站