逻辑应用程序中的用例
,我使用PANDAS DataFrame使用Azure函数创建一些数据。使用Azure函数后,我想在逻辑应用程序中进一步处理.xlsx格式的数据。因此,我需要Azure函数才能返回.xlsx文件。
问题
我无法格式化我的Azure函数的 httpresponse
,因此我可以在逻辑应用程序中进一步处理.xlsx文件。基本上,我需要从熊猫数据框架到httpresponse的正确转换。
怎么办?
该
import azure.functions as func
import logging
import pandas as pd
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
def main(req: func.HttpRequest) -> func.HttpResponse:
df = pd.DataFrame(np.random.randint(0, 100, size=(2, 4)), columns=list('ABCD'))
excel = convert_to_xlsx(df)
return func.HttpResponse(excel, status_code=200)
def convert_to_xlsx(df):
# Create excel representation
wb = Workbook()
sheet = wb.active
for row in dataframe_to_rows(df, index=False, header=True):
sheet.append(row)
logging.info('sheet: ' + str(list(sheet.values))) # So far, so good.
# Convert for HTTPResponse
res = ''
res = do_something(sheet) # <---- What to do here?
return res
在 convert_to_to_xlsx()
(见下文)中
>,效果很好。但是后来我不知道如何从这里继续转换 Workbook
。
另外,还有不支持.xlsx。基于该帖子,我尝试了以下内容,该帖子无法按预期工作:
def convert_to_xlsx(df):
# ... see above
# Only returns column names without values.
# Also, apparently not the right format?
return f'{[row for row in sheet]}'
一个选项可能是返回某种JSON响应,然后将其转换回逻辑应用程序中的Excel文件。但是我希望我可以跳过它,并立即从函数中返回一个.xlsx文件作为HTTP有效负载。
Use Case
Within a Logic App, I create some data using an Azure Function with a Pandas DataFrame. After employing the Azure Function, I want to further process the data in .xlsx format within the Logic App. Therefore I need the Azure Function to return an .xlsx file.
Problem
I am unable to format the HTTPResponse
of my Azure Function so that I can further process the .xlsx file within the Logic App. Basically I require the correct conversion from my Pandas DataFrame to the HTTPResponse.
What to do in convert_to_xlsx()
(see below) to achieve the desired output?
Toy Example
import azure.functions as func
import logging
import pandas as pd
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
def main(req: func.HttpRequest) -> func.HttpResponse:
df = pd.DataFrame(np.random.randint(0, 100, size=(2, 4)), columns=list('ABCD'))
excel = convert_to_xlsx(df)
return func.HttpResponse(excel, status_code=200)
def convert_to_xlsx(df):
# Create excel representation
wb = Workbook()
sheet = wb.active
for row in dataframe_to_rows(df, index=False, header=True):
sheet.append(row)
logging.info('sheet: ' + str(list(sheet.values))) # So far, so good.
# Convert for HTTPResponse
res = ''
res = do_something(sheet) # <---- What to do here?
return res
What I tried
I tried converting the data to openpyxl's Workbook
, which worked fine. But then I did not know how to proceed from here to convert from a Workbook
.
Also, there is this answer using xlrd, which I could not get to work for my use case. Additionally, xlrd does not support .xlsx anymore. Based on that post, I tried the following, which did not work as intended:
def convert_to_xlsx(df):
# ... see above
# Only returns column names without values.
# Also, apparently not the right format?
return f'{[row for row in sheet]}'
One option might be to return some kind of JSON response and then convert it back to an excel file within the logic app. But I hoped that I might be able to skip that and immediately return a .xlsx file from the function as HTTP payload.
发布评论
评论(2)
为了获得Excel文件,您还必须操纵标题,请参见 /a>。
没有更改的标头,您将获得我假设的ZIP对象。
因此,通过Azure函数返回Excelfelile的工作示例如下:
In order to obtain an Excel file you also have to manipulate the header, see https://stackoverflow.com/a/67276395/7641854.
Without the changed header you will obtain a zip object I assume.
Thus a working example to return an Excelfile via Azure function looks like this:
该方法可能是将输出写入缓冲区并在
httpresponse
中返回缓冲区的内容,但是,由于对通过HTTP返回大型文件的文件大小和执行时间的疑虑,我选择上传结果。通过使用(片段)之类的东西来表现到Azure Blob存储:
The approach could be to write the output to a buffer and return the buffer's content within the
HTTPResponse
However, due to concerns regarding file size and execution times for returning large files via http, I opted for uploading the resulting excel to an Azure BLOB storage, by using something like (snippet):