gpt4 book ai didi

python - 如何从azure blob存储编辑pdf而不将其下载到本地? (使用菲茨)

转载 作者:行者123 更新时间:2023-12-03 06:13:32 25 4
gpt4 key购买 nike

我的 Blob 存储中已有一个 pdf 文件。我需要突出显示其中的几行并将其存储为新的 pdf(再次存储在 blob 存储中)。我尝试在下面的链接中找到它,但找不到。下面是伪代码:

import fitz


def edit_pdfs(path_to_pdf_from_blob)

### READ pdf from blob storage
doc = fitz.open(path_to_pdf_from_blob)

## EDIT doc (fitz.fitz.Document) - I already have working code to edit the doc , but won't put it here to avoid complexity


### WRITE pdf to blob storage
doc.save(new_path_to_pdf_from_blob)

已经看到的答案:

Access data within the blob storage without downloading
How can I read a text file from Azure blob storage directly without downloading it to a local file(using python)?
Azure Blobstore: How can I read a file without having to download the whole thing first?

最佳答案

我在自己的环境中进行了尝试并得到了以下结果:

最初,我的容器中有一个名为 important.pdf 的 pdf 文档,内容如下。

enter image description here

您可以使用以下代码编辑pdf,而无需下载到本地。

代码:

from io import BytesIO
import fitz
from azure.storage.blob import BlobServiceClient

connection_string = "your-connection-string"
blob_name = "important.pdf"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client = blob_service_client.get_blob_client(container="test", blob=blob_name)

# Download the PDF file as bytes
pdf_bytes = blob_client.download_blob().content_as_bytes()
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
page = doc[0]
rect = fitz.Rect(50, 50, 200, 200)
highlight = page.add_highlight_annot(rect)
# Set the color of the highlight annotation
highlight.update()

new_blob_name = "demo.pdf"
modified_pdf_stream = BytesIO()
doc.save(modified_pdf_stream)
modified_pdf_bytes = modified_pdf_stream.getvalue()

# Get a BlobClient object for the new PDF file
new_blob_client = blob_service_client.get_blob_client(container="test", blob=new_blob_name)
new_blob_client.upload_blob(modified_pdf_bytes, overwrite=True)

#delete an original file
blob_client = blob_service_client.get_blob_client(container="test", blob=blob_name)
blob_client.delete_blob()

输出:

enter image description here

关于python - 如何从azure blob存储编辑pdf而不将其下载到本地? (使用菲茨),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76496911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com