Saving openpyxl file via text and filestream

前端 未结 5 1125
清酒与你
清酒与你 2021-02-01 16:49

I\'m building OpenPyXL into an application that expects a string containing the content of the excel file, for it to write via file stream.

From my investigation into th

相关标签:
5条回答
  • 2021-02-01 17:05
    from openpyxl import Workbook
    from io import BytesIO
    
    rows = [[1,2], [3,4]]
    
    book = Workbook()
    sheet = book.active
    
    for row in rows:
        sheet.append(row)
    
    io = BytesIO
    book.save(io)
    
    content = io.getValue()
    
    return Response(
        content,
        mimetype=magic.from_buffer(content, mime=True),
        headers={
        'Content-Disposition': 'attachment;filename=' + 'test.xlsx'}
    )
    
    0 讨论(0)
  • 2021-02-01 17:10

    What about using a StringIO object to save the contents of the file:

    from openpyxl.workbook import Workbook
    from StringIO import StringIO
    
    output = StringIO()
    wb = Workbook()
    wb.save(output)
    print output.getvalue()
    

    The string you're looking for is what is being printed in the last line of this example.

    0 讨论(0)
  • 2021-02-01 17:11

    In openpyxl 2.6 calling the save_virtual_workbook method issues the following warning:

    DeprecationWarning: Call to deprecated function save_virtual_workbook (Use a NamedTemporaryFile).
    

    At some point save_virtual_workbook will be removed from openpyxl.

    In Python 3 typical usage to save an openpyxl workbook to a filestream becomes:

    from io import BytesIO
    from tempfile import NamedTemporaryFile
    from openpyxl import Workbook
    
    wb = Workbook()
    with NamedTemporaryFile() as tmp:
        wb.save(tmp.name)
        output = BytesIO(tmp.read())
    
    

    After looking at the implementation of the WorkBook save method, the 'filename' is sent straight to ZipFile which accepts a path or file-like object so there is no need for a NamedTemporaryFile and simply use in-memory BytesIO:

    from io import BytesIO
    from openpyxl import Workbook
    
    wb = Workbook()
    virtual_workbook = BytesIO()
    wb.save(virtual_workbook)
    
    # now use virtual_workbook to send to a stream; email attachment, etc
    
    
    0 讨论(0)
  • 2021-02-01 17:17

    A compatible implementation with save_virtual_workbook deprecated since version 2.6:

    from io import BytesIO
    from tempfile import NamedTemporaryFile
    
    
    def save_virtual_workbook(workbook):
        with NamedTemporaryFile() as tf:
            workbook.save(tf.name)
            in_memory = BytesIO(tf.read())
            return in_memory.getvalue()
    
    0 讨论(0)
  • 2021-02-01 17:28

    jcollado's answer is actually valid, but there is also a function (sadly not documented yet) called "save_virtual_workbook" in openpyxl.writer.excel that will take your workbook and return the workbook as a string:

    from openpyxl.workbook import Workbook
    from openpyxl.writer.excel import save_virtual_workbook
    
    wb = Workbook()
    print save_virtual_workbook(wb)
    

    What you're looking for is the string returned by save_virtual_workbook()

    0 讨论(0)
提交回复
热议问题