Python和HTTP触发器在将工作簿转换为Base64或保存到临时文件夹后没有返回。

huangapple go评论74阅读模式
英文:

Python and http trigger not returning a workbook after converting to base64 or saving to temp folder

问题

我们正在尝试使用Azure HTTP触发器的func.httpResponse返回openpyxl的工作簿。在此之前,我们需要将其转换为字节或字节数组。

文件的类型是openpyxl.workbook.workbook.Workbook()

最佳方式是使用tempfile.NamedTemporaryFile。在查看文档时,我们已经执行了以下操作:

tempFilePath = tempfile.gettempdir()
fp = tempfile.NamedTemporaryFile()
customizedFile.save(fp)
filesDirListInTemp = listdir(tempFilePath)

其中我们的工作簿是customizedFile

我们无法检索文件及其路径,以便将路径用于上传到Blob存储。

我们尝试了以下方法:

with NamedTemporaryFile(mode='w') as tmp:
    customizedFile.save(tmp.name)
    output = io.BytesIO(tmp)
    return func.HttpResponse(output, status_code=200)

但是,我们收到了以下错误:

预期的是str、bytes或os.PathLike对象,而不是Workbook

我们尝试了几种选项,如转换为base64,但它不起作用,因为脚本期望的是字节或字节数组,而不是工作簿:

file_stream = io.BytesIO()
file = open(customizedFile, 'rb')
file.save(file_stream)
file_stream.seek(0)
base64_data = base64.b64encode(file_stream.getvalue()).decode('utf-8')

错误为:

预期的是str、bytes或os.PathLike对象,而不是Workbook

如何将工作簿上传到触发器目录中的临时文件夹中,将其转换为字节并将其作为HTTP响应返回?

英文:

We are trying to return a workbook of openpyxl using func.httpResponse of an azure http trigger. Before that we need to convert it into Bytes or byteArray.

the file is of type openpyxl.workbook.workbook.Workbook().

The best way is using tempfile.NamedTemporaryFile. While looking into documentations, we've done the following:

tempFilePath = tempfile.gettempdir()
fp = tempfile.NamedTemporaryFile()
customizedFile.save(fp)
filesDirListInTemp = listdir(tempFilePath)

Where our workbook is customizedFile.

We weren't able to retrieve the file and its path in order to use the path to upload into a blob storage.

We tried this:

with NamedTemporaryFile(mode='w') as tmp:
    customizedFile.save(tmp.name)
    output = io.BytesIO(tmp)
    return func.HttpResponse(output, status_code=200)

However, we got the following error:

> expected str, bytes or os.PathLike object, not Workbook

we tried several option like converting to base64 but it didn't work as the error was that the script expecting bytes or byteArray not workbook:

file_stream = io.BytesIO()
file =open(customizedFile, 'rb')
file.save(file_stream)
file_stream.seek(0)
base64_data = base64.b64encode(file_stream.getvalue()).decode('utf-8')

the error:

> expected str, bytes or os.PathLike object, not Workbook

how to upload the workbook into temporary folder within the directory of the trigger to convert it into bytes and return it with the http response?

答案1

得分: 0

我找到了以下解决方案:

wb = workbook.Workbook()
wb = customizedFile
virtual_workbook = io.BytesIO()
wb.save(virtual_workbook)
logging.warn('Workbook saved to virtual_workbook.')
return func.HttpResponse(
    virtual_workbook.getvalue(),
    mimetype='application/octet-stream',
    headers={
        'Content-Disposition': 'attachment;filename=' + customizedFileName}
)
英文:

I found out the solution as follows:

wb = workbook.Workbook()
wb = customizedFile
virtual_workbook = io.BytesIO()
wb.save(virtual_workbook)
logging.warn('Workbook saved to virtual_workbook.')
return func.HttpResponse (
    virtual_workbook.getvalue(),
    mimetype='application/octet-stream',
    headers={
    'Content-Disposition': 'attachment;filename=' + customizedFileName}
)

huangapple
  • 本文由 发表于 2023年2月18日 00:07:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定