作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 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 文档,内容如下。
您可以使用以下代码编辑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()
输出:
关于python - 如何从azure blob存储编辑pdf而不将其下载到本地? (使用菲茨),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76496911/
我收到此错误:ModuleNotFoundError:当我尝试在我的 docker 应用程序中导入 fitz 时,没有名为“前端”的模块。 尝试使用: fitz==0.0.1.dev2 PyMuPDF
我是一名优秀的程序员,十分优秀!