Sreepu
Sreepu

Reputation: 143

How to convert Azure Blob file CSV to Excel in Python

I'm trying to convert the CSV file which is there in the azure storage container to EXCEL and place it in the same container.

I'm able to read the data from a CSV file with the below code and am not able to convert it into excel and upload it into the container.

from io import StringIO
from azure.storage.blob import BlobServiceClient, ContainerClient, BlobClient
from typing import Container
import pandas as pd

conn_str = "DefaultEndpointsProtocol=https;AccountName="";AccountKey="";EndpointSuffix="""
container = "testing"
blob_name = "Test.csv"
filename = "test.xlsx"

container_client = ContainerClient.from_connection_string(
conn_str=conn_str, 
container_name=container
)   

downloaded_blob = container_client.download_blob(blob_name)

read_file = pd.read_csv(StringIO(downloaded_blob.content_as_text()) )

print(read_file)

Any suggestions on how can I achieve this?

Upvotes: 2

Views: 1221

Answers (1)

user459872
user459872

Reputation: 24562

You can convert the pandas Data Frame(read_file) to excel file using to_excel API. Since you want to upload it into the blob storage you can write it to the memory buffer first.

import pandas as pd
from io import BytesIO

buffer = BytesIO()

# By setting the 'engine' in the ExcelWriter constructor.
writer = pd.ExcelWriter(buffer, engine="xlsxwriter")
# 'read_file' is your pandas DataFrame
read_file.to_excel(writer, sheet_name="Sheet1") 

# Save the workbook
writer.save()

# Seek to the beginning 
buffer.seek(0)

buffer now has the data that is ready to be uploaded to the blob storage. So you can create a blob client instance first and use the upload_blob method to upload the excel file. Also set overwrite=True if you want to overwrite the file in Azure.

excel_blob_client = BlobClient.from_connection_string(
conn_str=conn_str, 
container_name=container,
blob_name = "test.xlsx"
)  
excel_blob_client.upload_blob(buffer, overwrite=True)

References

Upvotes: 2

Related Questions