2013-03-02 4 views
8

matplotlib 파일을 .tiff 이미지로 저장합니다. 그런 다음 Excel 파일을 열고 거기에 이미지를 붙여 넣을 수 있기를 바랍니다.matplotlib 그래프를 프로그래밍 방식으로 Excel에 삽입 할 수 있습니까?

openpyxl이 이미지 임베딩을 지원하지 않는 것 같습니다. xlwt는 bmp 만합니다.

프로그래밍 방식으로 tiff를 bmp로 변환 할 수 있다면 그 또한 도움이 될 수 있습니다.

아이디어 중 하나를 환영합니다. 그래프 내 부피가 작은 (약 10 파일 당)이다 유사한

그러나 BMP TIFF로 변환하는

Embed multiple jpeg images into EXCEL programmatically?

것은 허용.

답변

7

다음은 웹에서 완벽하게 작동하는 두 개의 서로 다른 링크에서 발견 한 것입니다.

from PIL import Image 

file_in = "image.png" 
img = Image.open(file_in) 
file_out = 'test1.bmp' 
print len(img.split()) # test 
if len(img.split()) == 4: 
    # prevent IOError: cannot write mode RGBA as BMP 
    r, g, b, a = img.split() 
    img = Image.merge("RGB", (r, g, b)) 
    img.save(file_out) 
else: 
    img.save(file_out) 

from xlwt import Workbook 
w = Workbook() 
ws = w.add_sheet('Image') 
ws.insert_bitmap(file_out, 0, 0) 
w.save('images.xls') 

코드의 이미지 부분은 여기 http://www.daniweb.com/software-development/python/threads/253957/converting-an-image-file-png-to-a-bitmap-file ENE URANS 응답에서이다 :하기 matplotlib 내가 여기의 사용을 만들 것입니다있는 PNG 파일을 저장 허용한다.

xlwt는 단순히 http://www.simplistix.co.uk/presentations/python-excel.pdf에서 찾은 xlwt의 문서 형식입니다.

1

Openpyxl은 실제로 이미지 포함을 지원하며 .png 또는 기존 .xlsx 파일을 사용하는 사용자에게 더 효과적 일 수 있습니다! 아래 코드는 input.xlsx의 셀 A1에 이미지를 추가하고 파일을 output.xlsx로 저장합니다.

import matplotlib.pyplot as plt 
import openpyxl 

# Your plot generation code here... 
plt.savefig("myplot.png", dpi = 150) 

wb = openpyxl.load_workbook('input.xlsx') 
ws = wb.active 

img = openpyxl.drawing.Image('myplot.png') 
img.anchor(ws.cell('A1')) 

ws.add_image(img) 
wb.save('output.xlsx') 
1

이 나를 위해 밖으로 일 :

import openpyxl 

wb = openpyxl.load_workbook('input.xlsx') 
ws = wb.active 

img = openpyxl.drawing.image.Image('myplot.png') 
ws.add_image(ws.cell('A1')) 

ws.save('output.xlsx') 
관련 문제