xlsxwriter:如何加载使用 matplotlib.pyplot 创建的 BytesIO png 文件
xlsxwriter: how to load a BytesIO png files created with matplotlib.pyplot
python==3.5.2,matplotlit==3.0.0,pandas0.24.0。 windows 10.
上的 numpy==1.15.4
当使用 matplotlib 的 savefig 保存 matplotlib 图形并使用 xlsxwriter 将其导出到 excel 文件时,有时保存到 png 文件的时间稍晚于导出到 excel 导致 png 文件"not be seen",因此不会导出到 excel。
我想到将图形保存到缓冲区中:
import numpy as np
import pandas as pd
import xlsxwriter
import matplotlib.pyplot as plt
from io import BytesIO
df = pd.DataFrame(np.random.random(size=(10, 3)),
columns=['a', 'b', 'c'])
# make plot
fig, ax = plt.subplots()
df.plot(ax =ax)
# save plot
buffy = BytesIO()
fig.savefig(buffy, format='png')
buffy.seek(0)
img_data = buffy.read()
# export plot to an xlsx file
wb = xlsxwriter.Workbook('test_BytesIO.xlsx')
ws = wb.add_worksheet('the_pic')
ws.insert_image('A1', 'image.png', {'image_data': img_data})
wb.close()
但是,我正在努力使用 xlsxwriter 将文件导出到 excel sheet。
任何线索将不胜感激。
编辑
@jmcnamara:添加任何文件名,例如 'image.png',也无济于事:
Traceback (most recent call last): File "C:...\binary_save_plt.py",
line 24, in
wb.close() File "C:\Users...\Python35\lib\site-packages\xlsxwriter\workbook.py", line
306, in close
self._store_workbook() File "C:\Users...\Python35\lib\site-packages\xlsxwriter\workbook.py", line
637, in _store_workbook
self._prepare_drawings() File "C:\Users...\Python35\lib\site-packages\xlsxwriter\workbook.py", line
1073, in _prepare_drawings
self._get_image_properties(filename, image_data) File "C:\Users...\Python35\lib\site-packages\xlsxwriter\workbook.py", line
1143, in _get_image_properties
data = image_data.getvalue() AttributeError: 'bytes' object has no attribute 'getvalue'
image_data
参数需要是一个 BytesIO 对象(不是来自它的数据)。您可以为图像指定任何合适的名称作为文件名。请参阅 insert_image().
上的文档
以下应该有效:
buffy = BytesIO()
fig.savefig(buffy, format='png')
wb = xlsxwriter.Workbook('test_BytesIO.xlsx')
ws = wb.add_worksheet('the_pic')
ws.insert_image('A1', 'image.png', {'image_data': buffy})
python==3.5.2,matplotlit==3.0.0,pandas0.24.0。 windows 10.
上的 numpy==1.15.4当使用 matplotlib 的 savefig 保存 matplotlib 图形并使用 xlsxwriter 将其导出到 excel 文件时,有时保存到 png 文件的时间稍晚于导出到 excel 导致 png 文件"not be seen",因此不会导出到 excel。 我想到将图形保存到缓冲区中:
import numpy as np
import pandas as pd
import xlsxwriter
import matplotlib.pyplot as plt
from io import BytesIO
df = pd.DataFrame(np.random.random(size=(10, 3)),
columns=['a', 'b', 'c'])
# make plot
fig, ax = plt.subplots()
df.plot(ax =ax)
# save plot
buffy = BytesIO()
fig.savefig(buffy, format='png')
buffy.seek(0)
img_data = buffy.read()
# export plot to an xlsx file
wb = xlsxwriter.Workbook('test_BytesIO.xlsx')
ws = wb.add_worksheet('the_pic')
ws.insert_image('A1', 'image.png', {'image_data': img_data})
wb.close()
但是,我正在努力使用 xlsxwriter 将文件导出到 excel sheet。 任何线索将不胜感激。
编辑
@jmcnamara:添加任何文件名,例如 'image.png',也无济于事:
Traceback (most recent call last): File "C:...\binary_save_plt.py", line 24, in wb.close() File "C:\Users...\Python35\lib\site-packages\xlsxwriter\workbook.py", line 306, in close self._store_workbook() File "C:\Users...\Python35\lib\site-packages\xlsxwriter\workbook.py", line 637, in _store_workbook self._prepare_drawings() File "C:\Users...\Python35\lib\site-packages\xlsxwriter\workbook.py", line 1073, in _prepare_drawings self._get_image_properties(filename, image_data) File "C:\Users...\Python35\lib\site-packages\xlsxwriter\workbook.py", line 1143, in _get_image_properties data = image_data.getvalue() AttributeError: 'bytes' object has no attribute 'getvalue'
image_data
参数需要是一个 BytesIO 对象(不是来自它的数据)。您可以为图像指定任何合适的名称作为文件名。请参阅 insert_image().
以下应该有效:
buffy = BytesIO()
fig.savefig(buffy, format='png')
wb = xlsxwriter.Workbook('test_BytesIO.xlsx')
ws = wb.add_worksheet('the_pic')
ws.insert_image('A1', 'image.png', {'image_data': buffy})