python - 如何使用 Python 从 azure blob 读取 docx 文件

标签 python azure ms-word azure-blob-storage

如何使用 Python 从 azure blob 读取 docx 文件? 我使用以下代码,但最后,blob_content 包含所有不可读的字符。此代码适用于 txt 文件,但不适用于 MS Word 文档 (*.docx)。

如果有解决办法请帮忙。

blob_service_client_instance = BlobServiceClient(account_url=STORAGEACCOUNTURL, credential=STORAGEACCOUNTKEY)
blob_client_instance = blob_service_client_instance.get_blob_client(container_name, blob_name, snapshot=None)
blob_download = blob_client_instance.download_blob()
blob_content = blob_download.readall().decode('utf-8')

最佳答案

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

最初,我尝试使用这段代码通过 Visual Studio 代码从 azure blob 存储读取 docx 文件。

在门户中,我在 azure blob 存储中有一个 docx 文件

enter image description here

from  azure.storage.blob  import  BlobServiceClient

client=BlobServiceClient.from_connection_string("<Connection string>")
serviceclient = client.get_container_client("test")
bc = serviceclient.get_blob_client(blob="sample.docx")
   with open("sample.docx", 'wb') as file:
data = bc.download_blob()
file.write(data.readall())

上述代码有效并从 azure blob 存储下载了 docx 文件。 当我尝试打开该文件时,它是源代码编辑器而不是 docx 代码编辑器。

控制台:

enter image description here

在我使用一段代码读取从 azure blob 存储下载的 docx 文件之后。

代码:

import  docx
doc = docx.Document("<path of the downloaded file >")
all_paras = doc.paragraphs
for  para  in  all_paras:
print(para.text)

控制台: 执行上述代码后,我能够成功读取docx文件。

enter image description here

关于python - 如何使用 Python 从 azure blob 读取 docx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74571122/

相关文章:

azure - 应用程序网关 URL 路由到多个应用程序服务

Excel Vba 转 Word : How to write a pagenumber into a textframe?

.net - 为什么在调试时 Word 会卡住?

python - 根据 groupby 值向 pandas 数据框添加一个新列

python - mysql.connector.errors.DatabaseError : 2005 (HY000): Unknown MySQL server host 'db' (2)

sql-server - 将Azure V12数据库导入本地SQL 2012

java - 用样式写入word文档

python从递归方法返回列表

python - 输入函数不起作用并返回 FileInput 对象

python - 如何优化 pandas 中的数据帧迭代?