【问题标题】:Is it possible to get a partial file with the open function?是否可以使用 open 函数获取部分文件?
【发布时间】:2022-01-09 23:22:01
【问题描述】:

我需要发送一份 PDF 文件以便使用 Microsoft Azure 阅读。我只需要解码长文档的第一页。我试图只发送第一页。以下是它目前的工作方式:

from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials

subscription_key = "my_key387309823"
endpoint = "my_endpoint82309483098420"

computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))

这是我想要改变的:

computervision_client.read_in_stream(open('mydocument.pdf', 'rb'), raw=True, pages=['1'])

这会将整个 PDF 发送到 Azure。是否可以使用open 功能剪切PDF 的第一页? open().read().split() 应该很容易,但这不是我可以发送 Azure 的格式。

【问题讨论】:

  • 有几个在 Python 中处理 PDF 文件的第三方库可能会对您有所帮助,请尝试查看 PyMuPDF(以前称为 fitz)或 PyPDF。使用内置的 open 方法只会给你一个文件 obj/bytestream 来处理,而普通 Python 没有任何处理 PDF 文件格式的上下文。

标签: python azure azure-cognitive-services


【解决方案1】:

不,open 函数不可能。您需要使用任何 PDF python 模块仅提取 PDF 的第一页并获取该 1 页 PDF 的字节流,然后您可以将其传递给计算机视觉模块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2014-08-29
    相关资源
    最近更新 更多