使用Python将图表从Excel导出为图像

使用Python将图表从Excel导出为图像,python,windows,excel,winapi,win32com,Python,Windows,Excel,Winapi,Win32com,我一直在尝试将Excel中的图表导出为Python中的图像文件(JPG或ING)。我正在看WIn32com。这是我到目前为止所拥有的 import win32com.client as win32 excel = win32.gencache.EnsureDispatch("Excel.Application") wb = excel.Workbooks.Open("<WORKSHEET NAME>") r = wb.Sheets("<SHEET NAME>").Rang

我一直在尝试将Excel中的图表导出为Python中的图像文件(JPG或ING)。我正在看WIn32com。这是我到目前为止所拥有的

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示例才能使其正常工作。虽然我讨厌回答我自己的问题,但我还是把这个留给可能需要它的人

    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()
将win32com.client作为win32导入
wb=excel.Workbooks.Open(excel\u文件)
selection=“A1:J30”
xl_范围=wb.Sheets().范围(选择)
excel.ActiveWorkbook.Sheets.Add(在=excel.ActiveWorkbook.Sheets(3)之后)。Name=“image\u sheet”
cht=excel.ActiveSheet.ChartObjects().Add(0,0,
xl_范围.宽度,xl_范围.高度)
xl_range.CopyPicture()
#将图表添加到新工作表
cht.Chart.Paste()
#将带有图表的工作表导出到新文件
cht.Chart.Export()
#删除工作表
cht.Delete()
excel.ActiveSheet.Delete()
#合上书
excel.active工作簿.Close()

我知道这是一个老问题,但它帮助我走上了正确的道路,所以我回来分享我完成的脚本,该脚本在工作表中查找所有图表并将其导出为.png。 上面的脚本可以工作,但由于它只是复制工作表中的一个范围,因此您需要依赖于恰好位于该位置的图形

    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

对我来说,这很有效:

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)

现在我会推荐excel2img图书馆,为我服务得很好。确保已安装(pip安装excel2img)

如果不需要网格线,只需将其隐藏在excel中即可


git rep:了解更多信息。

我尝试使用下面的命令,但excel中没有更新任何内容。文件是否以只读方式打开? 代码可能有什么问题

import win32com.client as win32
from win32com.client import Dispatch
import os
xlApp = Dispatch('Excel.Application')
workbook = xlApp.Workbooks.Open(r'C:\Users\eshsubh\Documents\EKN\DL MS1.xls')
xlApp.Sheets("Sheet2").Select()
xlSheet1 = xlApp.Sheets(1)
xlApp.DisplayAlerts = False
i = 0
for chart in xlSheet1.ChartObjects():
 print (chart.Name)
 chart.CopyPicture()
 #Create new temporary sheet
xlSheet1.ActiveWorkbook.Sheets.Add(After=xlSheet1.ActiveWorkbook.Sheets(3)).Name="temp_sheet" + str(i)
xlSheet1 = sheet_ranges.ActiveSheet
#Add chart object to new sheet.
cht = xlSheet1.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  

我的建议是打破与Excel的联系。为什么在Excel中绘制图表,然后使用Python?使用python读取数据并使用matplotlib进行绘图很容易。不幸的是,之前所做的工作迫使我坚持使用Excel。还有10多个图表是从多张图纸绘制的。为什么不直接导出这些图表呢?
Sheet
对象有一个
ChartObjects
集合:每个
ChartObject
都有一个包含
Chart
Export
方法。复制包含图表的区域,然后将其粘贴到空图表中似乎需要很长的时间。是否有方法可以导出到特定路径?我无法为此更改输出路径,是否有方法更改?Yomajo excel2img不适用于microsoft excel 2016。你能推荐一个替代方案吗?请在单独的主题中提问,而不是在其他答案上提问。您的问题可能会更受关注,并对社区有所帮助。
import excel2img

excel2img.export_img("test.xlsx", "test.gif", "Sheet1", "MyNamedRange")
import win32com.client as win32
from win32com.client import Dispatch
import os
xlApp = Dispatch('Excel.Application')
workbook = xlApp.Workbooks.Open(r'C:\Users\eshsubh\Documents\EKN\DL MS1.xls')
xlApp.Sheets("Sheet2").Select()
xlSheet1 = xlApp.Sheets(1)
xlApp.DisplayAlerts = False
i = 0
for chart in xlSheet1.ChartObjects():
 print (chart.Name)
 chart.CopyPicture()
 #Create new temporary sheet
xlSheet1.ActiveWorkbook.Sheets.Add(After=xlSheet1.ActiveWorkbook.Sheets(3)).Name="temp_sheet" + str(i)
xlSheet1 = sheet_ranges.ActiveSheet
#Add chart object to new sheet.
cht = xlSheet1.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