save excel file stream to azure blob storage

oeliger

I programmed a few lines of code in Python which opens an Excel file from a Azure Blob Storage with the openpyxl-library. The code is running in Azure Functions.

After a few modifications on the content, I will create a second workbook and copy the content from the original workbook into it. I saved the second workbook as a stream. Now I want to save the second workbook back to the blob storage as an Excel file again (other container).

    import logging
    from typing import Container
    
    import azure.functions as func
    import azure.storage.blob
    import openpyxl as xl
    import io
    

    ### Start Trigger
    def main(myblob: func.InputStream):
        logging.info(f"Python blob trigger function processed blob \n"
                     f"Name: {myblob.name}\n"
                     f"Blob Size: {myblob.length} bytes")
        logging.info('Loading Workbook...')
        ### Load Excel file
        wb1 = xl.load_workbook(filename=io.BytesIO(myblob.read()))
        logging.info(wb1.sheetnames)
        
        ### Create Second Workbook
        output = io.BytesIO()
        wb2 = xl.Workbook()
        wb2.active
        wb2.create_sheet('TestSheet')
        wb2.save(output)

        ### Upload to Azure Blob Storage
        blob_service_client = azure.storage.blob.BlobServiceClient.from_connection_string(conString)
        blob_client = blob_service_client.get_blob_client(container='test2', blob='test2.xlsx')
        blob_client.upload_blob(output)

When I now run the code everything will work. But the excel file is corrupt on the blob storage and I get the following error when I try to open the Excel file: enter image description here

Thanks for helping!

Frank Gong

The problem seems to be here: wb2.save(output), you can refer to the following code:

import logging
import azure.functions as func
import azure.storage.blob
import openpyxl as xl
import io
from tempfile import NamedTemporaryFile


def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")
    #logging.info(myblob.read())
    logging.info('Loading Workbook...')
    ### Load Excel file
    wb1 = xl.load_workbook(filename=io.BytesIO(myblob.read()))
    ws1 = wb1.worksheets[0]
    logging.info(wb1.sheetnames)

    ### Create Second Workbook
    #output = io.BytesIO()
    wb2 = xl.Workbook()
    ws2 = wb2.create_sheet('TestSheet')

    # calculate total number of rows and 
    # columns in source excel file
    mr = ws1.max_row
    mc = ws1.max_column
    
    # copying the cell values from source 
    # excel file to destination excel file
    for i in range (1, mr + 1):
        for j in range (1, mc + 1):
            # reading cell value from source excel file
            c = ws1.cell(row = i, column = j)
    
            # writing the read value to destination excel file
            ws2.cell(row = i, column = j).value = c.value

    ### Upload to Azure Blob Storage
    conString = ""
    blob_service_client = azure.storage.blob.BlobServiceClient.from_connection_string(conString)
    blob_client = blob_service_client.get_blob_client(container='testout', blob='test2.xlsx')
    with NamedTemporaryFile() as tmp:
        wb2.save(tmp.name)
        output = io.BytesIO(tmp.read())
        blob_client.upload_blob(output)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Save .txt file into a Azure Blob storage

unable to upload a excel file into Azure Storage Blob

Upload a file stream to AZURE BLOB STORAGE using REST APIs

Downloading file from Azure blob storage via Memory Stream

adding to azure blob storage with stream

Azure Blob Storage - 404 when i save to file

Azure blob storage save Dto

Save RData to Azure Blob Storage

How to read an excel file stored in Azure Storage as a Blob File

How to get an excel file from web and store it in an azure blob storage

save created excel file to azure blob container. path issue

upload file in azure blob storage

Saving a file into Azure Blob storage

HLS Event Stream from Azure Blob Storage

Python script to use data from Azure Storage Blob by stream, and update blob by stream without local file reading and uploading

File Storage Solution Architecture - Azure Blob Storage

Stream 'Azure Blob Storage' file URL to Amazon S3 bucket

How to download a blob file from Azure Storage and save it to an FTP server using Powershell?

Why does Ctrl-S disable the save button on Azure blob storage file editor?

How to Upload excel file in a Azure blob storage using Azure Functions with HTTP trigger?

Download a file from Azure Blob File Storage

Move a File from Azure File Storage to Azure Blob Storage

File ID in Azure Blob Storage [AZURE-BLOB]

Download a excel file from azure blob and process its data without needing to save file to local directory

Create copy of file on Azure Blob Storage

Simultaneous upload of same file to Azure Blob Storage

Is it possible to recover an azure blob storage file that was overwritten?

Azure Blob Storage vs. File Service

Azure Blob Storage Temporary File URL