openpyxl - TypeError: a bytes-like object is required, not '_io.BytesIO'
openpyxl - TypeError: a bytes-like object is required, not '_io.BytesIO'
使用我编写的 openpyxl 和 excel 二进制对象中的文件,但是当我尝试通过 "write" 方法保存它时,出现此异常:
Traceback (most recent call last): File "stack.py", line 13, in
file.write(output) TypeError: a bytes-like object is required, not '_io.BytesIO'
我该如何解决这个问题?下面是一个简单的例子:
import openpyxl
from io import BytesIO
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "TEST"
output = BytesIO()
wb.save(output)
file = open("ciao.xlsx", "wb")
file.write(output) # <--- this instruction doesn't work..
file.close()
注意:我已经知道我可以直接用openpyxl保存excel文件,但是在我的真实代码中,我需要将它保存在一个二进制文件中。
您可以使用 getvalue 函数从 BytesIO 输出中获取类似字节的对象。
file.write(output.getvalue())
使用我编写的 openpyxl 和 excel 二进制对象中的文件,但是当我尝试通过 "write" 方法保存它时,出现此异常:
Traceback (most recent call last): File "stack.py", line 13, in file.write(output) TypeError: a bytes-like object is required, not '_io.BytesIO'
我该如何解决这个问题?下面是一个简单的例子:
import openpyxl
from io import BytesIO
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "TEST"
output = BytesIO()
wb.save(output)
file = open("ciao.xlsx", "wb")
file.write(output) # <--- this instruction doesn't work..
file.close()
注意:我已经知道我可以直接用openpyxl保存excel文件,但是在我的真实代码中,我需要将它保存在一个二进制文件中。
您可以使用 getvalue 函数从 BytesIO 输出中获取类似字节的对象。
file.write(output.getvalue())