2012-06-19 143 views
2

我一直試圖將圖表從Excel導出爲Python中的圖像文件(JPG或ING)。我正在看WIn32com。這是我迄今爲止所擁有的。使用Python將圖表從Excel導出爲圖像

import win32com.client as win32 
excel = win32.gencache.EnsureDispatch("Excel.Application") 
wb = excel.Workbooks.Open("<WORKSHEET NAME>") 
r = wb.Sheets("<SHEET NAME>").Range("A1:J50") 
# Here A1:J50 is the area over which cart is 
r.CopyPicture() 

這是我卡住的地方。我需要現在將所選範圍複製到一個文件。任何幫助或指向文檔的指針可以幫助我很多。

我已經仿照基於以下VBA腳本上面的代碼:

Sub Export_Range_Images() 
    ' ========================================= 
    ' Code to save selected Excel Range as Image 
    ' ========================================= 
    Dim oRange As Range 
    Dim oCht As Chart 
    Dim oImg As Picture 

    Set oRange = Range("A1:B2") 
    Set oCht = Charts.Add 
    oRange.CopyPicture xlScreen, xlPicture 
    oCht.Paste 
    oCht.Export FileName:="C:\temp\SavedRange.jpg", Filtername:="JPG" 
End Sub 

代碼段來自:http://vbadud.blogspot.com/2010/06/how-to-save-excel-range-as-image-using.html

+0

我的建議是打破與Excel您聯繫。爲什麼在Excel中製圖並使用Python?使用python讀取數據並使用matplotlib繪圖很容易。 – TJD

+0

不幸的是,之前完成的工作迫使我堅持使用Excel。並且有多張圖表可以從多張圖表中繪製出來。 – Parikshit

+0

爲什麼不直接導出圖表? 'Sheet'對象有一個'ChartObjects'集合:每個'ChartObject'都有一個包含'Chart'和'Export'方法。複製包含圖表的範圍,然後將其粘貼到空白圖表中似乎需要很長時間。 –

回答

2

我不得不看一些例子VBA得到這個工作。雖然我討厭回答我自己的問題,但我會將此留給那些可能需要它的人。

import win32com.client as win32 
    wb = excel.Workbooks.Open(excel_file) 
    selection = "A1:J30" 
    xl_range = wb.Sheets(<sheet_name>).Range(selection) 
    excel.ActiveWorkbook.Sheets.Add(     After=excel.ActiveWorkbook.Sheets(3)).Name="image_sheet" 
    cht = excel.ActiveSheet.ChartObjects().Add(0,0, 
              xl_range.Width, xl_range.Height) 
    xl_range.CopyPicture() 
    # add the chart to new sheet 
    cht.Chart.Paste() 
    # Export the sheet with the chart to a new file 
    cht.Chart.Export(<image_filename>) 
    # Delete the sheet 
    cht.Delete() 
    excel.ActiveSheet.Delete() 
    # Close the book 
    excel.ActiveWorkbook.Close() 
+0

雖然這可能是一個很好的參考點,但該腳本無法正常工作。 – Patrick

3

我知道這是一個老問題,但它有助於把我在正確的軌道上,所以我回來和大家分享我完成腳本,發現在工作表中所有的圖表和出口他們作爲巴紐。 上面的腳本可以工作,但由於它只是在工作表內複製一個範圍,所以您要根據圖形恰好在那個位置。

import win32com.client as win32 
    from win32com.client import Dispatch 
    import os 

    xlApp = Dispatch('Excel.Application') 

    workbook = xlApp.Workbooks.Open("Book1.xls") 
    xlApp.Sheets("Sheet1").Select() 

    xlSheet1 = xlApp.Sheets(1) 

    #WARNING: The following line will cause the script to discard any unsaved changes in your workbook 
    #Ensure to save any work before running script 
    xlApp.DisplayAlerts = False 

    i = 0 
    for chart in xlSheet1.ChartObjects(): 
     print chart.Name 

     chart.CopyPicture() 
     #Create new temporary sheet 
     xlApp.ActiveWorkbook.Sheets.Add(After=xlApp.ActiveWorkbook.Sheets(3)).Name="temp_sheet" + str(i) 
     temp_sheet = xlApp.ActiveSheet 

     #Add chart object to new sheet. 
     cht = xlApp.ActiveSheet.ChartObjects().Add(0,0,800, 600) 
     #Paste copied chart into new object 
     cht.Chart.Paste() 
     #Export image 
     cht.Chart.Export("chart" + str(i) + ".png") 

     #This line is not entirely neccessary since script currently exits without saving 
     temp_sheet.Delete() 
     i = i+1 

    xlApp.ActiveWorkbook.Close() 
    #Restore default behaviour 
    xlApp.DisplayAlerts = True 
0

對於我這個行之有效:

from win32com.client import Dispatch 

app = Dispatch("Excel.Application") 
workbook_file_name = 'Programmes.xlsx' 
workbook = app.Workbooks.Open(Filename=workbook_file_name) 

# WARNING: The following line will cause the script to discard any unsaved changes in your workbook 
app.DisplayAlerts = False 

i = 1 
for sheet in workbook.Worksheets: 
    for chartObject in sheet.ChartObjects(): 
     # print(sheet.Name + ':' + chartObject.Name) 
     chartObject.Chart.Export("chart" + str(i) + ".png") 
     i += 1 

workbook.Close(SaveChanges=False, Filename=workbook_file_name) 

或者這樣:

from win32com.client import Dispatch 

app = Dispatch("Excel.Application") 
workbook_file_name = 'Programmes.xlsx' 
workbook = app.Workbooks.Open(Filename=workbook_file_name) 
app.DisplayAlerts = False 
try: 
    workbook.SaveAs(Filename="ExcelCharts.htm", FileFormat=44) # 44 = html file format 
except Exception as ex: 
    print(ex) 
finally: 
    workbook.Close(SaveChanges=False, Filename=workbook_file_name)