[英]Python: how to read a doc file from azure blob storage?
我在 blob 存儲中有一個 docx 文件。
我嘗試做的是獲取 blob 中文件的鏈接/路徑或 url 以應用此 function:
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())
感謝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:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.