【问题标题】:Is there a way to work with files in python without downloading the files有没有办法在不下载文件的情况下使用 python 中的文件
【发布时间】:2020-09-12 23:37:06
【问题描述】:

我正在用 python 编写代码以下载 pdf 文件和 png 文件,然后从文件中提取信息。但是,我必须使它与 Microsoft-Azure 兼容,它不允许我在阅读文件之前保存文件。有没有一种简单的方法可以在我读取文件时将文件保存在内存中,而无需保存文件?

【问题讨论】:

  • 您使用的是 azure blob?
  • 目前我没有使用 azure blob,但如果这可以解决问题,我可能会开始使用它。最好的解决方案是没有 azure blob 的解决方案
  • 您使用的是什么版本的 Python SDK?
  • 我不知道。我对 Azure 完全陌生,那么我该如何解决呢?
  • 另外,请编辑您的问题并包含您目前编写的代码。

标签: python azure pdf png


【解决方案1】:

我通过使用诸如此类的临时文件找到了解决方案。顺便说一句,我使用花粉。

with tempfile.TemporaryFile() as fp:
    for chunk in pollenfile.iter_content(chunk_size=1024):
        # writing one chunk at a time to pdf file, because pdf is so large
        if chunk:
            fp.write(chunk)

    pdfReader = PyPDF2.PdfFileReader(
        fp
    )  # Discerning the number of pages will allow us to parse through all the pages.
    num_pages = pdfReader.numPages
    count = 0
    pollen_txt = ""  # The while loop will read each page.
    while count < num_pages:
        pageObj = pdfReader.getPage(count)
        count += 1
        pollen_txt += (
            pageObj.extractText()
        )  

通过这样做,我以字符串格式提取 pdf,而无需将其保存到我的计算机中,以便以后进行操作。

【讨论】:

    猜你喜欢
    • 2019-10-01
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 2020-08-31
    • 2015-06-26
    • 2013-10-24
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多