Excel的win32com编程与Python。将参数传递给函数 [英] Excel win32com programming with Python. Passing Parameters to function
问题描述
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屋!