Saving openpyxl file via text and filestream

前端 未结 5 1187
清酒与你
清酒与你 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: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()
    

提交回复
热议问题