0

I am looking to simply download an excel blob from Azure Storage, make some modifications and then upload it with a new name using Azure Python SDK v12. I've found several questions on here using v2.x with blockblobservice module. But that is not available in v12. An additional constraint is that I do not want anything locally as this is part of a serverless app.

Here is my code and notations:

from azure.storage.blob import BlobClient
import io
import pandas as pd

#Download Blob: works fine
download_client=BlobClient.from_connection_string(
        conn_str=az_str, 
        container_name=container_name, 
        blob_name=blob_name) 
read = io.BytesIO()
download_stream = download_client.download_blob()
df = pd.read_excel(download_stream.readall())

#Make arbitrary changes: works fine
df_new = df.append(df)
print(df_new)

#Upload Blob: blob is empty
new_blob_name=f'UploadTest.xlsx'
upload_client = BlobClient.from_connection_string(
        conn_str=az_str, 
        container_name=container_name, 
        blob_name=new_blob_name)

write = io.BytesIO()
df_new.to_excel(write, sheet_name='Test Sample') 
upload_client.upload_blob(write, blob_type="BlockBlob", overwrite=True)

1 Answer 1

1

Please try by setting the stream's position to 0. Something like:

write = io.BytesIO()
df_new.to_excel(write, sheet_name='Test Sample')
write.seek(0)
upload_client.upload_blob(write, blob_type="BlockBlob", overwrite=True)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that did it!
I have an additional question that might not be worth another post, would it be possible to do the above with an additional to_excel, for an additional sheet in the same excel book?
I would recommend asking that as a separate question please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.