使用tempfile在Flask中创建PDF/XLS文档

3
我想询问是否有可能创建PDF / XLS文档作为临时文件。我这样做是为了随后使用flask发送它们。对于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文件中。

你能否发布一下你目前的代码示例? - matthewatabet
@matthewatabet,好的。请查看更新后的帖子。 - Rail Suleymanov
谢谢!我的答案使用持久化临时文件应该可以解决问题。 - matthewatabet
2个回答

3

使用tempfile.mkstemp()方法可以在磁盘上创建一个标准的临时文件,该文件会一直保留直到被删除:

import tempfile
import os

handle, filepath = tempfile.mkstemp()
f = os.fdopen(handle)  # convert raw handle to file object
...

编辑 tempfile.TemporaryFile()会在它关闭后被销毁,这就是为什么你上面的代码会失败的原因。


1
谢谢,这是一个很好的起点。我在这里找到了更多信息:https://www.logilab.org/blogentry/17873 - Rail Suleymanov
或者使用tempfile.NamedTemporaryFile() - moogoo

0

你可以使用上下文管理器(或者atexit模块)来使用和删除NamedTemporaryFile。它可以为你完成一些繁琐的工作。
示例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:\users\fll2cj\AppData\Local\Temp\tmpyv37sp58 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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接