0

I am creating a function in Azure function with the following bindings:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "payroll-excel",
      "connection": "Payroll_test_connection"
    },
    {
      "name": "inputblob",
      "type": "blob",
      "path": "payroll-excel/{queueTrigger}",
      "connection": "Payroll_test_connection",
      "direction": "in"
    }
  ]
}

The function is triggered by a message in a que which contains the path to an Excel file located in a blob container. Then my init.py is as follows:

import logging
import pandas as pd
import os,io
import azure.functions as func


def main(msg: func.QueueMessage,
        inputblob: func.InputStream) -> None:
    
    filename=msg.get_body().decode('utf-8')
    logging.info('Python queue trigger function processed a queue item: %s',
                 filename)
    
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {inputblob.name}\n"
                 f"Blob Size: {inputblob.length} bytes")
    Raw_file = inputblob.read()
    logging.info (type(Raw_file))
    df = pd.read_excel(io.BytesIO(Raw_file))
    logging.info (df.head())

I am trying to read the excel file to use it in my program but I am getting the following error:

System.Private.CoreLib: Result: Failure
Exception: BadZipFile: Bad magic number for central directory
Stack:   File "/usr/lib/azure-functions-core-tools/workers/python/3.6/LINUX/X64/azure_functions_worker/dispatcher.py", line 312, in _handle__invocation_request
self.__run_sync_func, invocation_id, fi.func, args)
File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/lib/azure-functions-core-tools/workers/python/3.6/LINUX/X64/azure_functions_worker/dispatcher.py", line 431, in __run_sync_func
return func(**params)
File "/mnt/c/repos////__init__.py", line 19, in main
df = pd.read_excel(io.BytesIO(Raw_file))
File "/mnt/c/repos///.venv/lib/python3.6/site-packages/pandas/util/_decorators.py", line 296, in wrapper
return func(*args, **kwargs)
File "/mnt/c/repos///.venv/lib/python3.6/site-packages/pandas/io/excel/_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine)
File "/mnt/c/repos///.venv/lib/python3.6/site-packages/pandas/io/excel/_base.py", line 867, in __init__
self._reader = self._engines[engine](self._io)
File "/mnt/c/repos///.venv/lib/python3.6/site-packages/pandas/io/excel/_xlrd.py", line 22, in __init__
super().__init__(filepath_or_buffer)
 File "/mnt/c/repos///.venv/lib/python3.6/site-packages/pandas/io/excel/_base.py", line 351, in __init__
self.book = self.load_workbook(filepath_or_buffer)
File "/mnt/c/repos///.venv/lib/python3.6/site-packages/pandas/io/excel/_xlrd.py", line 35, in load_workbook
return open_workbook(file_contents=data)
File "/mnt/c/repos///.venv/lib/python3.6/site-packages/xlrd/__init__.py", line 115, in open_workbook
zf = zipfile.ZipFile(timemachine.BYTES_IO(file_contents))
File "/usr/lib/python3.6/zipfile.py", line 1131, in __init__
self._RealGetContents()
File "/usr/lib/python3.6/zipfile.py", line 1226, in _RealGetContents
raise BadZipFile("Bad magic number for central directory")

Any Idea about what I need to do??

Thanks

  • What happens if you directly do `pd.read_excel(inputblob)` since `inputblob` is already a stream descendent. – krishg Sep 08 '20 at 05:24

0 Answers0