Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
刘靖钰刘容榕王润昕任雪的作业.py 1.60 KB
Copy Edit Raw Blame History
刘靖钰 authored 2023-11-25 07:37 . liurongrong teamwork
import os
import pdfplumber
def pdf2txt(ThePath:str,aim_path:str ):
def parsePDF(dir, file, pdf_path, txtpath):
try:
with open(txtpath, "w", encoding='utf-8') as txt:
with pdfplumber.open(pdf_path) as pdf:
for i, page in enumerate(pdf.pages):
print(f'正在转换{dir}-{file}-{i + 1}页')
txt.write(page.extract_text())
except Exception as e:
print(f"发生异常: {str(e)}")
# 主程序
ThePath = r'/Users/liujingyu/Downloads/gitee/cipin/3/pdf/2007年报'
aim_path = r'/Users/liujingyu/Downloads/gitee/cipin/3/txt'
dir_list = os.listdir(ThePath)
for dir in dir_list:
dir_path = os.path.join(ThePath, dir) # 使用os.path.join来构建目录路径
if not os.path.exists(os.path.join(aim_path, dir)):
os.makedirs(os.path.join(aim_path, dir)) # 使用os.path.join来构建目录路径
for root, dirs, files in os.walk(dir_path, topdown=False):
for file in files:
try:
pdf_path = os.path.join(root, file) # 使用os.path.join来构建文件路径
file_name = os.path.basename(pdf_path) # 从文件路径中提取文件名
txt_path = os.path.join(aim_path, dir, file_name.split('.')[0] + '.txt') # 构建txt文件路径
parsePDF(dir, file, pdf_path, txt_path)
except:
with open('错误信息.txt', 'a', encoding='UTF-8', errors='ignore') as f:
f.write(os.path.join(root, file) + '/n') # 使用os.path.join来构建文件路径
pdf2txt(ThePath,aim_path)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化