Size: 1913
Comment:
|
← Revision 4 as of 2009-12-25 07:09:35 ⇥
Size: 2060
Comment: converted to 1.6 markup
|
Deletions are marked like this. | Additions are marked like this. |
Line 9: | Line 9: |
attachment:excelfunc.GIF | {{attachment:excelfunc.GIF}} |
Line 61: | Line 61: |
= 反馈 = * 如果是函数图表制作, gunplot 等等优秀软件比使用 Excel 要方便和快捷的多哪………… ZoomQuiet |
一个简单的用win32com调用Excel做函数图像的例子,本程序使用剪贴板向Excel传输数据。 调用参数:functions min max points
- functions是一个用逗号隔开的函数列表,自变量名是x,中间不能有空格。
- min是x轴的最小值
- max是x轴的最大值
- points是描绘图像的点数
例如:draw.py sin(x)/x,sin(x)/x*0.5*(1-cos(2*3.1415926*(x+20)/40)) -20 20 1000
1 # com client for excel
2 from math import *
3 from win32com.client import Dispatch
4 from win32com.client import constants
5 from sys import *
6 from string import *
7 import win32clipboard as w
8
9 def evalFunc(func, x):
10 try: return eval(func)
11 except: return ""
12
13 funcs, xmin, xmax, points = argv[1], float(argv[2]), float(argv[3]), int(argv[4])
14 funclist = funcs.split(",")
15 xdata = [float(x)/points * (xmax - xmin) + xmin for x in range(0,points)]
16 ydata = [ [evalFunc(func, x) for x in xdata] for func in funclist]
17
18 xlApp = Dispatch("Excel.Application")
19 xlApp.Visible = True
20 xlApp.Workbooks.Add()
21 sheet = xlApp.ActiveWorkbook.ActiveSheet
22
23 for n, f in enumerate(["x"] + funclist):
24 sheet.Cells(1,n+1).Value = f
25
26 def setClipText(aString):
27 w.OpenClipboard()
28 w.EmptyClipboard()
29 w.SetClipboardText(aString)
30 w.CloseClipboard()
31
32 def setExcelText(sheet, x, y, clipstr):
33 sheet.Cells(x,y).Select()
34 setClipText(clipstr)
35 sheet.Paste()
36
37 for n, data in enumerate([xdata] + ydata):
38 setExcelText(sheet, 2, n+1, "\n".join(map(str, data)))
39
40 xlApp.Charts.Add()
41 chart = xlApp.ActiveChart
42 chart.ChartType = constants.xlXYScatterSmoothNoMarkers
43 range = "A1:%s%d" % (ascii_uppercase[len(funclist)],points+1)
44 chart.SetSourceData (Source=sheet.Range(range), PlotBy=constants.xlColumns)
45 chart.Location(constants.xlLocationAsObject, sheet.Name)
46 #xlApp.Visible = True
反馈
如果是函数图表制作, gunplot 等等优秀软件比使用 Excel 要方便和快捷的多哪………… ZoomQuiet