如何使用python将图表/图表添加到工作表/ excelsheet中

问题描述:

我已经使用pychart创建了图表,但输出是以pdf/png/ps等形式出现的。但我需要这些图表应该进入excel片。什么是将这个图表提取到工作表的方法。如何使用python将图表/图表添加到工作表/ excelsheet中

from pychart import * 
import sys 
def to_percents(data): 
    total = float(sum(v for _, v in data)) 
    data[:] = [(k, v/total) for k, v in data] 
    return data 

data = [("foo", 10),("bar", 20), ("baz", 30), ("ao", 40)] 
theme.use_color = True 
theme.get_options()  
ar = area.T(size=(150,150), legend=legend.T(), 
      x_grid_style = None, y_grid_style = None) 

plot = pie_plot.T(data=data, arc_offsets=[0,0,0,0], 
        shadow = (0, 0, fill_style.gray50), 
        label_offset = 25, 
        arrow_style = arrow.a3) 
ar.add_plot(plot) 
ar.draw() 

尝试使用win32com包

from win32com import client 
excel=client.Dispatch("Excel.Application") 
excel.Visible=True 
book=excel.Workbooks.Open("myfile.csv", False, True) 
sheet=book.Worksheets(1) 
chart=book.Charts.Add() 

然后,修改图表,你想

+0

感谢您的信息,我试图安装Win32和启动代码,但它是给错误> >>进口win32com.client >>> XL = win32com.client.Dispatch( “Excel.Application”) 回溯(最近通话最后一个): 文件 “”,1号线,在 在调度hAndUserName return(_GetGoodDispatch(IDispatch,clsctx),userName)中的文件“c:\ Python27 \ DLLs \ win32com \ client \ __ init__.py”,第95行 文件“c:\ Python27 \ DLLs \ win32com \ client \ dynamic.py“,第72行,在_GetGoodDispat有什么问题在这里请帮助我 – 2012-04-05 09:26:16

+0

感谢Maksym,它的工作原理,感谢支持 – 2012-06-13 06:08:18

+0

@Shashi因此请接受答案:-) – 2012-06-13 10:25:13