如何从包含在不同文件夹中的docx文件中提取文本
how to extract text from docx files contaning in different folders
我正在编写代码以从扩展名为 docx 的 word 文档中提取文本。我有一个名为 "EXTRACTION" 的大文件夹,该文件夹包含不同的子文件夹(例如:文件夹 1、2、3 等),每个子文件夹包含 2 到 10 个 docx 文档。我想从每个文件中提取文本并将其放入一个新的 txt 文件中。
我开始编写这段代码,但它不起作用(代码的第二个版本):
import os
import glob
import docx
print(os.getcwd())
dirs = dirs = glob.glob('fi*')
path = os.getcwd()
for directory in dirs:
for filename in directory:
if filename.endswith(".docx") or filename.endswith(".doc"):
document = docx.Document(filename)
#docText = []
with open('your_file.txt', 'w') as f:
for paragraph in document.paragraphs:
if paragraph.text:
#docText.append(paragraph.text)
f.write("%s\n" % paragraph.text)
这段代码好像不行,能不能帮我改进一下
在您的代码中,directory
只是一个字符串;所以 for filename in directory
只是循环遍历 f
、i
、c
、h
、i
、e
、r
等等
此外,您在每次迭代时都覆盖了 your_file.txt
。您想打开它一次,然后遍历您从中提取的文档。
import glob
import os
import docx
with open('your_file.txt', 'w') as f:
for directory in glob.glob('fi*'):
for filename in glob.glob(os.path.join(directory, "*")):
if filename.endswith((".docx", ".doc")):
document = docx.Document(filename)
for paragraph in document.paragraphs:
if paragraph.text:
#docText.append(paragraph.text)
f.write("%s\n" % item)
您正在使用 item
而未声明它,所以这里仍然存在错误;我猜不出你希望这个变量包含什么,所以我就让它保持原样在你的原始代码中。
您可以使用 glob.glob 从子目录中获取所有文件的列表
files = [file for file_list in [glob.glob('/path/to/mainfolder/**/{}'.format(x),recursive=True) for x in ('*.doc','*.docx')] for file in file_list]
with open('your_file.txt', 'w') as f:
for file in files:
document = docx.Document(filename)
for paragraph in document.paragraphs:
if paragraph.text:
f.write("%s\n" % item)
我正在编写代码以从扩展名为 docx 的 word 文档中提取文本。我有一个名为 "EXTRACTION" 的大文件夹,该文件夹包含不同的子文件夹(例如:文件夹 1、2、3 等),每个子文件夹包含 2 到 10 个 docx 文档。我想从每个文件中提取文本并将其放入一个新的 txt 文件中。
我开始编写这段代码,但它不起作用(代码的第二个版本):
import os
import glob
import docx
print(os.getcwd())
dirs = dirs = glob.glob('fi*')
path = os.getcwd()
for directory in dirs:
for filename in directory:
if filename.endswith(".docx") or filename.endswith(".doc"):
document = docx.Document(filename)
#docText = []
with open('your_file.txt', 'w') as f:
for paragraph in document.paragraphs:
if paragraph.text:
#docText.append(paragraph.text)
f.write("%s\n" % paragraph.text)
这段代码好像不行,能不能帮我改进一下
在您的代码中,directory
只是一个字符串;所以 for filename in directory
只是循环遍历 f
、i
、c
、h
、i
、e
、r
等等
此外,您在每次迭代时都覆盖了 your_file.txt
。您想打开它一次,然后遍历您从中提取的文档。
import glob
import os
import docx
with open('your_file.txt', 'w') as f:
for directory in glob.glob('fi*'):
for filename in glob.glob(os.path.join(directory, "*")):
if filename.endswith((".docx", ".doc")):
document = docx.Document(filename)
for paragraph in document.paragraphs:
if paragraph.text:
#docText.append(paragraph.text)
f.write("%s\n" % item)
您正在使用 item
而未声明它,所以这里仍然存在错误;我猜不出你希望这个变量包含什么,所以我就让它保持原样在你的原始代码中。
您可以使用 glob.glob 从子目录中获取所有文件的列表
files = [file for file_list in [glob.glob('/path/to/mainfolder/**/{}'.format(x),recursive=True) for x in ('*.doc','*.docx')] for file in file_list]
with open('your_file.txt', 'w') as f:
for file in files:
document = docx.Document(filename)
for paragraph in document.paragraphs:
if paragraph.text:
f.write("%s\n" % item)