加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
第12周作业-魏旭颖-2301066062.py 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
winnie6733 提交于 2023-11-29 10:39 . 审计专硕 魏旭颖 作业
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings
import os
import pdfplumber
def parsePDF(pdf_path, txt_path):
try:
with open(txt_path, "w", encoding='utf-8') as txt:
with pdfplumber.open(pdf_path) as pdf:
for i, page in enumerate(pdf.pages):
print(f'正在转换{pdf_path}-{i + 1}页')
txt.write(page.extract_text())
except Exception as e:
print(f"发生异常: {str(e)}")
pdf_dir = '/Users/liujingyu/Downloads/gitee/cipin/3/pdf'
txt_dir = '/Users/liujingyu/Downloads/gitee/cipin/3/txt'
for root, dirs, files in os.walk(pdf_dir):
for file in files:
if file.endswith('.pdf'):
pdf_path = os.path.join(root, file)
relative_path = os.path.relpath(pdf_path, pdf_dir)
txt_path = os.path.join(txt_dir, os.path.splitext(relative_path)[0] + '.txt')
try:
parsePDF(pdf_path, txt_path)
except Exception as e:
with open('错误信息.txt', 'a', encoding='UTF-8', errors='ignore') as f:
f.write(f"{pdf_path}: {str(e)}\n")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化