我想询问是否有可能创建PDF / XLS文档作为临时文件。我这样做是为了随后使用flask发送它们。对于pdf / xls文件的创建,我分别使用reportlab和xlsxwriter软件包。当我使用它们的方法保存文档时,会出现“Python临时文件权限被拒绝”错误。当我尝试使用tempfile方法关闭文件时,文件会变得损坏。有没有办法克服这个问题?或者其他合适的解决方案?
编辑:
一些代码片段:
类似的事情也发生在PDF文件中。
编辑:
一些代码片段:
import xlswriter
import tempfile
from flask import after_this_request
@app.route('/some_url', method=['POST'])
def create_doc_function():
@after_this_request
def cleanup(response):
temp.close()
return response
temp = tempfile.TemporaryFile()
book = xlsxwriter.Workbook(temp.name)
# some actions here ...
book.close() # raises "Python temporaty file permission denied" error.
# If missed, Excel book is gonna be corrupted,
# i.e. blank, which make sense
return send_file(temp, as_attachment=True,
attachment_filename='my_document_name.xls')
类似的事情也发生在PDF文件中。