我想问一下是否可以将PDF/XLS文档创建为临时文件。我这样做是为了让他们事后使用烧瓶。对于pdf/xls文件的创建,我分别使用reportlab和xlsxwriter包。当我使用它们的方法保存文档时,我得到"Python临时文件权限被拒绝"错误。当我尝试使用tempfile方法关闭时,文件会损坏。有办法克服这个吗?或者其他合适的解决方案?
编辑:一些代码片段:
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文件也是如此
使用tempfile.mkstemp()
将在磁盘上创建一个标准的临时文件,该文件将一直存在,直到删除:
import tempfile
import os
handle, filepath = tempfile.mkstemp()
f = os.fdopen(handle) # convert raw handle to file object
...
编辑tempfile.TemporaryFile()
一旦关闭就会被破坏,这就是为什么上面的代码失败的原因。
您可以使用和删除NamedTemporaryFile与上下文管理器(或atexit模块)。它可能会帮你干脏活。
示例1:
import os
from tempfile import NamedTemporaryFile
# define class, because everyone loves objects
class FileHandler():
def __init__(self):
'''
Let's create temporary file in constructor
Notice that there is no param (delete=True is not necessary)
'''
self.file = NamedTemporaryFile()
# write something funny into file...or do whatever you need
def write_into(self, btext):
self.file.write(btext)
def __enter__(self):
'''
Define simple but mandatory __enter__ function - context manager will require it.
Just return the instance, nothing more is requested.
'''
return self
def __exit__(self, exc_type, exc_val, exc_tb):
'''
Also define mandatory __exit__ method which is called at the end.
NamedTemporaryFile is deleted as soon as is closed (function checks it before and after close())
'''
print('Calling __exit__:')
print(f'File exists = {os.path.exists(self.file.name)}')
self.file.close()
print(f'File exists = {os.path.exists(self.file.name)}')
# use context mamager 'with' to create new instance and do something
with FileHandler() as fh:
fh.write_into(b'Hi happy developer!')
print(f'nIn this point {fh.file.name} does not exist (exists = {os.path.exists(fh.file.name)})')
输出:Calling __exit__:
File exists = True
File exists = False
In this point D:usersfll2cjAppDataLocalTemptmpyv37sp58 does not exist (exists = False)
或者您可以使用atexit模块,当程序(cmd)退出时调用定义的函数。
示例2:
import os, atexit
from tempfile import NamedTemporaryFile
class FileHandler():
def __init__(self):
self.file = NamedTemporaryFile()
# register function called when quit
atexit.register(self._cleanup)
def write_into(self, btext):
self.file.write(btext)
def _cleanup(self):
# because self.file has been created without delete=False, closing the file causes its deletion
self.file.close()
# create new instance and do whatever you need
fh = FileHandler()
fh.write_into(b'Hi happy developer!')
# now the file still exists, but when program quits, _cleanup() is called and file closed and automaticaly deleted.