【问题标题】:Python: how to read a doc file from azure blob storage?Python:如何从 azure blob 存储中读取 doc 文件?
【发布时间】:2021-10-18 14:37:02
【问题描述】:

我在 blob 存储中有一个 docx 文件。

我尝试做的是获取 blob 中文件的链接/路径或 url 以应用此功能:

def get_docx_text(path):
    """
    Take the path of a docx file as argument, return the text in unicode.
    """
    document = zipfile.ZipFile(path)
    xml_content = document.read('word/document.xml')
    document.close()
    tree = XML(xml_content)

    paragraphs = []
    for paragraph in tree.getiterator(PARA):
        texts = [node.text
                 for node in paragraph.getiterator(TEXT)
                 if node.text]
        if texts:
            paragraphs.append(''.join(texts))

    text = '\n\n'.join(paragraphs)
    return (paragraphs,text)

在def get_docx_text(path)的参数路径中我想放文件的路径。

我该怎么做?

我尝试了类似的方法但不起作用:

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

connection_string='...'
blob_service_client = BlobServiceClient.from_connection_string(connection_string)

service_client = BlobServiceClient.from_connection_string(connection_string)

client = service_client.get_container_client("name_container")

bc = client.get_blob_client(blob="bronze/txt_name.docx")

with open("txt_name.docx", 'wb') as file:

    data = bc.download_blob()

    file.write(data.readall())

【问题讨论】:

  • 您遇到了什么问题?
  • ResourceNotFoundError:指定的 blob 不存在。但我想这不是获取路径的好方法
  • 方法正确。您能否检查一下您指定的名称 (bronze/txt_name.docx) 是否确实存在于 blob 容器中?
  • 它是 sink/bronze/txt_name.docx 但我该怎么做才能将此文件的路径提供给 def get_docx_text(path) 函数?
  • 请尝试bc = client.get_blob_client(blob="sink/bronze/txt_name.docx")

标签: python azure-blob-storage


【解决方案1】:

感谢 Gaurav 在评论中提供您的建议,并将其转换为帮助其他社区成员的答案。

问题: ResourceNotFoundError: The specified blob does not exist.

解决方案:请尝试使用此代码

bc = client.get_blob_client(blob="sink/bronze/txt_name.docx")

由于您是在运行代码的同一文件夹中下载 blob,因此您只需指定保存文件的名称。

例如:在这段代码中

with open("txt_name.docx", 'wb') as file:

【讨论】:

    猜你喜欢
    • 2012-06-16
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多