使用Python从Azure函数返回Azure函数的Excel文件

发布于 2025-02-07 19:02:05 字数 1868 浏览 2 评论 0 原文

逻辑应用程序中的用例

,我使用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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

预谋 2025-02-14 19:02:06

为了获得Excel文件,您还必须操纵标题,请参见 /a>。

没有更改的标头,您将获得我假设的ZIP对象。

因此,通过Azure函数返回Excelfelile的工作示例如下:

import pandas as pd
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:

    d = {'col1': [1, 2], 'col2': [3, 4]}
    df = pd.DataFrame(data=d)

    buffer = io.BytesIO()
    excel_buf = df.to_excel(buffer, index=False)

    return func.HttpResponse(
        buffer.getvalue(),
        headers={"Content-Disposition": 'attachment; filename="test.xlsx"'},
        mimetype='application/vnd.ms-excel',
        status_code=200,
    )

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:

import pandas as pd
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:

    d = {'col1': [1, 2], 'col2': [3, 4]}
    df = pd.DataFrame(data=d)

    buffer = io.BytesIO()
    excel_buf = df.to_excel(buffer, index=False)

    return func.HttpResponse(
        buffer.getvalue(),
        headers={"Content-Disposition": 'attachment; filename="test.xlsx"'},
        mimetype='application/vnd.ms-excel',
        status_code=200,
    )
掩于岁月 2025-02-14 19:02:06

该方法可能是将输出写入缓冲区并在 httpresponse 中返回缓冲区的内容,

...
from io import BytesIO

def main(req: func.HttpRequest) -> func.HttpResponse:    
    df = pd.DataFrame(np.random.randint(0, 100, size=(2, 4)), columns=list('ABCD'))
    buffer = BytesIO()
    excel_buf = df.to_excel(buffer)
    return func.HttpResponse(buffer.getvalue(), status_code=200)

但是,由于对通过HTTP返回大型文件的文件大小和执行时间的疑虑,我选择上传结果。通过使用(片段)之类的东西来表现到Azure Blob存储:

...
out_blob = BlobClient.from_connection_string(...)
excel_buf = df.to_excel(buffer)
out_blob.upload_blob(buffer.getvalue())
...

The approach could be to write the output to a buffer and return the buffer's content within the HTTPResponse

...
from io import BytesIO

def main(req: func.HttpRequest) -> func.HttpResponse:    
    df = pd.DataFrame(np.random.randint(0, 100, size=(2, 4)), columns=list('ABCD'))
    buffer = BytesIO()
    excel_buf = df.to_excel(buffer)
    return func.HttpResponse(buffer.getvalue(), status_code=200)

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):

...
out_blob = BlobClient.from_connection_string(...)
excel_buf = df.to_excel(buffer)
out_blob.upload_blob(buffer.getvalue())
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文