当前位置: 首页 > news >正文

pdf文件流或者本地文件读取

import requests
import pdfplumber
import io
import os
def read_pdf(path,proxies={},timeout=(3.2,10),download_image=False,headers={}):
f=''
if path=='' or type(path)!=str:
print("路径为空或格式不对!")
if path[0:4]=="http":
try:
#data=request.urlopen(path,timeout=10).read()
print(proxies)
print(headers)
data=requests.get(url=path,timeout=timeout,proxies=proxies,headers=headers)
print(data)
with open('99.pdf','wb+') as f2:
f2.write(data.content)
f = io.BytesIO(data.content)
except Exception as e:
print(e,"打开链接失败")
return ''
else:
try:
path=urllib.parse.unquote(path)
path=path.replace('file:///','').replace('/','\\')
f=open(path,'rb')
except Exception as e:
print(e,"打开本地文件失败")

    text=''
old_path=os.getcwd()
if download_image:
im_path=path.replace('https://','').replace("http://",'')
os.makedirs(im_path, exist_ok=True)
os.chdir(im_path)

with pdfplumber.open(f) as pdf:
# 遍历每个页面
for page in pdf.pages:
# 获取当前页面的全部文本信息,包括表格中的文字,没有内容则打印None
text+=page.extract_text()
if download_image:
images=page.images
i=0
for img in images:
f_img=open('{}.png'.format(i),'wb+')
f_img.write(img['stream'].get_data())
f_img.close()
i+=1
os.chdir(old_path)
f.close()
return text


url='https://www.airuniversity.af.edu/Portals/10/ASPJ/journals/Volume-27_Issue-6/V-Soine-Harker-Heminger-Scherrer.pdf'
proxies={'http':'192.168.1.122:1080','https':'192.168.1.122:1080'}
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0',\
#'cookie':'.ASPXANONYMOUS=txzams-d65KJ-plLxuK7ohwAwg6cmLo5fJzCbD3i4CaFY7sH2EYFw2jvP3bw64iuYiVJbpNQBxFLir7s-_8p65Huzw9Sab4REdtsGyvLi4E0hge-0; _ga=GA1.1.509430065.1753846772; dnn_IsMobile=False; ARRAffinity=7604675fe895ac43d4eee5ed64a571e723c5cb50da2e00ebe078cb3d6f359b1c; _ga_CSLL4ZEK4L=GS2.1.s1755051758$o5$g1$t1755052205$j60$l0$h0; _ga_313558765=GS2.1.s1755051759$o5$g1$t1755052205$j60$l0$h0',\
'sec-fetch-dest':'document'}
read_pdf(url,proxies=proxies,headers=headers)

http://www.lryc.cn/news/620759.html

相关文章:

  • 掌握MATLAB三维可视化:从基础到实战技巧
  • OpenCV Canny 边缘检测
  • 【js】让项目支持倾听和朗读AI技术
  • OpenCV图像平滑处理方法详解
  • tp5集成elasticsearch笔记
  • 开疆智能Ethernet转ModbusTCP网关连接UR机器人配置案例
  • ComfyUI工作流不动了?
  • OpenCV 形态学操作
  • Spring AI PagePdfDocumentReader 全解析:基于 Apache PdfBox 的按页 PDF 读取实战
  • COLMAP进行密集重建,三维重建的步骤
  • [机器学习]08-基于逻辑回归模型的鸢尾花数据集分类
  • AUTOSAR汽车电子嵌入式编程精讲300篇-【自动驾驶】硬件在环(HIL)(二)
  • 第四天~在CANFD或CAN2.0的ARXML文件中实现Multiplexor多路复用信号实战
  • 依托AR远程协助,沟通协作,高效流畅
  • 读From GPT-2 to gpt-oss: Analyzing the Architectural Advances
  • 第四天-创建一个Classic CAN(经典CAN2.0)/CANFD的系统描述ARXML文件
  • IDEA、Pycharm、DataGrip等激活破解冲突问题解决方案之一
  • 学习设计模式《二十二》——职责链模式
  • 深入了解linux系统—— 线程概念
  • 深入解析 Spring IOC 容器在 Web 环境中的启动机制
  • 嵌入式学习Day27
  • stm32项目(29)——基于stm32的智能眼镜设计
  • 【代码随想录day 20】 力扣 108.将有序数组转换为二叉搜索树
  • SwiftUI 页面弹窗操作
  • Linux网络编程:应用层自定义协议与序列化
  • Flutter sqflite插件
  • 支付域——账户系统设计
  • 支持pcm语音文件缓存顺序播放
  • 解剖HashMap的put <四> jdk1.8
  • OpenCv(二)——边界填充、阈值处理