admin管理员组

文章数量:1130349

用途:批量提取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(file_paths)

第四步:

# 导入docx库(主要)
from docx import Document
# 其他用于设置字体等信息所需

用途:批量提取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(file_paths)

第四步:

# 导入docx库(主要)
from docx import Document
# 其他用于设置字体等信息所需

本文标签: 文件批量pdfWord