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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:14:42  来源:igfitidea点击:

Python 2.7 - win32com.client - Move a worksheet from one workbook to another

pythonpython-2.7excel-vbawin32comvba

提问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_py
from 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:

以下是您可能希望在代码中更改的一些内容:

  1. You can use win32com.client.DispatchExto create a new instance of Excel to avoid interfering with any open Excel instances. If you use DispatchExyou can drop setting .Visibleto False. Further reading about DispatchExhere: http://timgolden.me.uk/python/win32_how_do_i/start-a-new-com-instance.html

  2. \ is an escape character. Use either raw strings or forward-slashes, e.g.: wbG=excel.Workbooks.Open(r'C:\Full Path\WorkbookB.xlsx')or wbG=excel.Workbooks.Open('C:/Full Path/WorkbookB.xlsx')

  1. 您可以使用win32com.client.DispatchEx来创建 Excel 的新实例,以避免干扰任何打开的 Excel 实例。如果您使用,DispatchEx您可以将设置删除.VisibleFalse. 进一步阅读DispatchEx这里:http: //timgolden.me.uk/python/win32_how_do_i/start-a-new-com-instance.html

  2. \ 是转义字符。使用原始字符串或正斜杠,例如: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:

结合这些建议,代码变为:

##代码##