我需要从 azure 中的 blob 容器下载 PDF 作为下载流 (StorageStreamDownloader) 并在 PDFPlumber 和 PDFminer 中打开它。我开发了将它们作为文件加载的所有要求,但我无法收到下载流(StorageStreamDownloader)并成功打开它。我是这样打开PDF的:
pdf = pdfplumber.open(pdfpath) //for pdfplumber
fp = open('Pdf/' + fileGlob, 'rb') // for pdfminer
parser = PDFParser(fp)
document = PDFDocument(parser)
Run Code Online (Sandbox Code Playgroud)
但是,我需要能够下载流。将 pdf 作为文件下载的代码片段:
blob_client = container.get_blob_client(remote_file)
with open(local_file_path,"wb") as local_file:
download_stream = blob_client.download_blob()
local_file.write(download_stream.readall())
local_file.close()
Run Code Online (Sandbox Code Playgroud)
我尝试了几个选项,甚至使用了一个没有运气的临时文件。有任何想法吗?
download_blob()将 blob 下载到一个StorageStreamDownloader 类中,在这个类中有一个download_to_stream,通过它您将获得 blob 流。
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
from io import BytesIO
import PyPDF2
filename = "test.pdf"
container_name="test"
blob_service_client = BlobServiceClient.from_connection_string("connection string")
container_client=blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(filename)
streamdownloader=blob_client.download_blob()
stream = BytesIO()
streamdownloader.download_to_stream(stream)
fileReader = PyPDF2.PdfFileReader(stream)
print(fileReader.numPages)
Run Code Online (Sandbox Code Playgroud)
这就是我的结果。它将打印 pdf 页码。
| 归档时间: |
|
| 查看次数: |
2534 次 |
| 最近记录: |