title: save excel file stream to azure blob storage
body: <p>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.</p>
  <p>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).</p>
  <pre><code>    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&quot;Python blob trigger function processed blob \n&quot;
                       f&quot;Name: {myblob.name}\n&quot;
                       f&quot;Blob Size: {myblob.length} bytes&quot;)
          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)
  </code></pre>
  <p>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:
  <a href="https://i.stack.imgur.com/rC0d0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rC0d0.png" alt="enter image description here" /></a></p>
  <p>Thanks for helping!</p>