如何使用 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 文件
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 代码编辑器。
控制台:
在我使用一段代码读取从 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文件。
关于python - 如何使用 Python 从 azure blob 读取 docx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74571122/