用途:批量提取pdf中的文本信息
主要使用库:pdfplumber、os、docx
第一步:定义函数:输入PDF文件的文件路径,即可获取文本
# 导入PDF所在的文件路径,返回文本信息
import pdfplumber
def pdf2txt(pdf_path):
txt = ''
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
txt = txt + page.extract_text()
return txt
第二步:定义函数:获取文件夹中的文件路径,并保存在列表,需输入的是PDF所在文件夹
#获取文件夹中的文件位置
import os
def get_file_paths(folder):
file_paths = []
for root, dirs, files in os.walk(folder):
for file in files:
file_path = os.path.join(root, file)
file_paths.append(file_path)
return file_paths
第三步:填写保存PDF文件的文件夹路径,获取PDF文件路径
# 填写要提取文件路径的文件夹路径
folder_path = r"文件路径"
file_paths = get_file_paths(folder_path)
print(f