pythondocx批量提取目录及内容_使用pythondocx搜索目录中的所有docx文件(批处理)...

这段代码展示了如何使用os.walk遍历指定文件夹(包括子目录)中的所有.docx文件,并加载到Document对象中进行处理。它首先创建一个空列表来存储.docx文件路径,然后通过os.walk遍历文件,检查文件是否为.docx类型,如果是,则将其添加到列表。之后,代码遍历列表中的每个文件路径,读取文档内容,提取表格中的特定单元格文本。此过程适用于需要批量处理Word文档的情况。
摘要由CSDN通过智能技术生成

如何循环所有文件将取决于您的项目可交付成果。所有的文件都在一个文件夹里吗?是否不止.docx个文件?在

为了解决所有问题,我们假设有子目录和其他文件与.docx文件混合在一起。为此,我们将使用^{}和{a2}import os

from docx import Document

# First, we'll create an empty list to hold the path to all of your docx files

document_list = []

# Now, we loop through every file in the folder "G:\GIS\DESIGN\ROW\ROW_Files\Docx"

# (and all it's subfolders) using os.walk(). You could alternatively use os.listdir()

# to get a list of files. It would be recommended, and simpler, if all files are

# in the same folder. Consider that change a small challenge for developing your skills!

for path, subdirs, files in os.walk(r"G:\GIS\DESIGN\ROW\ROW_Files\Docx"):

for name in files:

# For each file we find, we need to ensure it is a .docx file before adding

# it to our list

if os.path.splitext(os.path.join(path, name))[1] == ".docx":

document_list.append(os.path.join(path, name))

# Now create a loop that goes over each file path in document_list, replacing your

# hard-coded path with the variable.

for document_path in document_list:

document = Document(document_path) # Change the document being loaded each loop

table = document.tables[0]

project_cell = table.rows[2].cells[2]

paragraph = project_cell.paragraphs[0]

project = paragraph.text

print project

如需进一步阅读,请参阅^{}上的文档。在

另外,最好将代码放入可重用的函数中,但这对您自己也是一个挑战!在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值