vba Python 2.7 - win32com.client - 将工作表从一个工作簿移动到另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26935793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 2.7 - win32com.client - Move a worksheet from one workbook to another
提问by Stephanie
I'm trying to move one excel worksheet from workbook A to workbook B with python 2.7, but I keep getting an error.
我正在尝试使用 python 2.7 将一个 Excel 工作表从工作簿 A 移动到工作簿 B,但我一直收到错误消息。
Python script:
蟒蛇脚本:
import win32com.client
excel=win32com.client.Dispatch('Excel.Application')
excel.Visible=False
wbP=excel.Workbooks.Open('C:\Full Path\WorkbookA.xlsx')
wbG=excel.Workbooks.Open('C:\Full Path\WorkbookB.xlsx')
wbG.Worksheets("Sheet1").Select
wbG.Worksheets("Sheet1").Move(before=wbP.Worksheets("Annual"))
wbP.SaveAs('C:\Full Path\WorkbookA.xlsx')
excel.Application.Quit()
Error I'm receiving:
我收到的错误:
Traceback (most recent call last):
File "C:\Full Path\test.py", line 10, in <module>
wbG.Worksheets("Sheet1").Select
File "C:\Python27\lib\site-packages\win32com\gen_pyfrom win32com.client import DispatchEx
excel = DispatchEx('Excel.Application')
wbP=excel.Workbooks.Open(r'C:\Full Path\WorkbookA.xlsx')
wbG=excel.Workbooks.Open(r'C:\Full Path\WorkbookB.xlsx')
# note altered sheet name; also .Select is not required
wbG.Worksheets("Charts").Move(Before=wbP.Worksheets("Annual"))
wbP.SaveAs(r'C:\Full Path\WorkbookA.xlsx')
excel.Quit()
del excel # ensure Excel process ends
020813-0000-0000-C000-000000000046x0x1x8\Sheets.py", line 120, in __call__
ret = self._oleobj_.InvokeTypes(0, LCID, 2, (9, 0), ((12, 1),),Index
com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)
Thank you!
谢谢!
Solution:
解决方案:
See comments from bernie. The worksheet I needed moved was named Charts not Sheet1.
见伯尼的评论。我需要移动的工作表被命名为 Charts 而不是 Sheet1。
回答by mechanical_meat
I'm writing the comments up in an answer because it's easier to read...
我将评论写在答案中,因为它更容易阅读......
Since the error occurs on that line it appears that the problem is that there is no "Sheet1" in WorkbookB.xlsx
由于错误发生在该行上,因此问题似乎是 WorkbookB.xlsx 中没有“Sheet1”
Below are some things you might want to change in the code:
以下是您可能希望在代码中更改的一些内容:
You can use
win32com.client.DispatchEx
to create a new instance of Excel to avoid interfering with any open Excel instances. If you useDispatchEx
you can drop setting.Visible
toFalse
. Further reading aboutDispatchEx
here: http://timgolden.me.uk/python/win32_how_do_i/start-a-new-com-instance.html\ is an escape character. Use either raw strings or forward-slashes, e.g.:
wbG=excel.Workbooks.Open(r'C:\Full Path\WorkbookB.xlsx')
orwbG=excel.Workbooks.Open('C:/Full Path/WorkbookB.xlsx')
您可以使用
win32com.client.DispatchEx
来创建 Excel 的新实例,以避免干扰任何打开的 Excel 实例。如果您使用,DispatchEx
您可以将设置删除.Visible
到False
. 进一步阅读DispatchEx
这里:http: //timgolden.me.uk/python/win32_how_do_i/start-a-new-com-instance.html\ 是转义字符。使用原始字符串或正斜杠,例如:
wbG=excel.Workbooks.Open(r'C:\Full Path\WorkbookB.xlsx')
或wbG=excel.Workbooks.Open('C:/Full Path/WorkbookB.xlsx')
Incorporating those suggestions the code becomes:
结合这些建议,代码变为:
##代码##