Excel的win32com编程与Python。将参数传递给函数 [英] Excel win32com programming with Python. Passing Parameters to function

查看:156
本文介绍了Excel的win32com编程与Python。将参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑这个问题有一个简单的答案,但我无法找到它!我正在编写一个功能来绘制excel中的大量数据,我正在使用Python控制绘图。我已经生成了所有的图表,每个图表都是自己的图表,但是它们是无序的。 VBA代码是:

  Sheets(Chart1)。Move After:= Sheets(3)

这将采取Chart1,并在第3张表后移动它。在Python中如何做到这一点?我知道代码是这样的:

  xlChart.Move(之前,之后)

如何告诉python之前和之后的参数?我想大的事情是我没有想出如何在win32com编程中传递参数。



提前感谢任何帮助!

解决方案

这是一个非常简单的例子:

 从win32com import客户端

excel = client.Dispatch(Excel.Application)
excel.Visible = True
book = excel.Workbooks.Open(c:/ desktop / test。 xlsx,False,True)
sheet = book.Worksheets(1)
chart = book.Charts.Add()
chart.SetSourceData(sheet.Range($ A:$ B ))

chart.Move(Before = book.Worksheets(Sheet2))
chart.Move(book.Worksheets(Sheet2))#相当于上一行
chart.Move(book.Worksheets(Sheet2),无)#also等价

chart.Move(After = book.Worksheets(Sheet2))
chart.Move (无,book.Worksheets(Sheet2))#相当于上一行

我试过显示处理可选参数的各种选项,这些参数是重要的问题与Excel COM编程。您可以使用位置参数传递,并从末尾省略参数。或者您可以通过等价的。最后,您可以使用命名参数。


I suspect there is an easy answer for this question but I can't find it anywhere! I am writing a function to plot a lot of data in excel and I am controlling the plotting with Python. I have generated all of the charts and each chart is its own sheet, but they are out of order. The VBA code for this is:

Sheets("Chart1").Move After:=Sheets(3)

This will take "Chart1" and move it after the 3rd sheet. How do I do this in Python? I know that the code is something along the lines of:

xlChart.Move(Before,After)

How do I tell python the Before and After parameters? I guess the big thing is I haven't figured out how to pass parameters in win32com programming.

Thanks in advance for any help!

解决方案

Here's a very simple example:

from win32com import client

excel=client.Dispatch("Excel.Application")
excel.Visible=True
book=excel.Workbooks.Open("c:/desktop/test.xlsx", False, True)
sheet=book.Worksheets(1)
chart=book.Charts.Add()
chart.SetSourceData(sheet.Range("$A:$B"))

chart.Move(Before=book.Worksheets("Sheet2"))
chart.Move(book.Worksheets("Sheet2"))#equivalent to previous line
chart.Move(book.Worksheets("Sheet2"), None)#also equivalent

chart.Move(After=book.Worksheets("Sheet2"))
chart.Move(None,book.Worksheets("Sheet2"))#equivalent to previous line

I've tried to show the various options for dealing with optional parameters which are important issues with Excel COM programming. You can use positional parameter passing and omit parameters from the end. Or you can pass None which is equivalent. Finally, you can used named parameters.

这篇关于Excel的win32com编程与Python。将参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆