简体   繁体   English

Python:使用Win32com打开Excel工作簿时出错

[英]Python: error opening excel workbook with win32com

I am trying to make a program to open existing excel files, in order to read and write their contents. 我正在尝试制作一个程序来打开现有的excel文件,以便读取和写入其内容。 I am already having trouble opening files. 我已经无法打开文件了。

My code: 我的代码:

def open_sheet():
    excel = win32com.client.Dispatch("Excel.Application")
    wb = excel.Workbooks(r'C:\Users\<username>\Documents\<more file path>\test.xls').Open()
    ws = wb.Worksheets('abc') # there is a worksheet called abc.
    excel.Visible = True

Note: I abbreviated the file paths for brevity. 注意:为了简洁起见,我缩写了文件路径。 The .py file and the test.xls file are in the same folder. .py文件和test.xls文件位于同一文件夹中。

The error: 错误:

Traceback (most recent call last):
  File "C:\Users\<username>\Documents\<more file path>\automation code.py", line 37, in <module>
    open_sheet()
  File "C:\Users\<username>\Documents\<more file path>\automation code.py", line 33, in open_sheet
    wb = excel.Workbooks(r'C:\Users\<username>\Documents\<more file path>\test.xls').Open()
  File "C:\Users\<username>\AppData\Local\Programs\Python\Python36-32\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x9\Workbooks.py", line 198, in __call__
    ret = self._oleobj_.InvokeTypes(0, LCID, 2, (13, 0), ((12, 1),),Index
pywintypes.com_error: (**-2147352567**, '例外が発生しました。', (0, None, None, None, 0, -2147352565), None)

Note: I am using a Japanese computer. 注意:我使用的是日文计算机。 The sentence in Japanese means 'Exception occurred'. 日语中的句子意思是“发生异常”。

I tried to find out more about the error: 我试图找出有关该错误的更多信息:

import win32api
win32api.FormatMessage(**-2147352567**)

Output:
'例外が発生しました。\r\n'
(The translation, I think, is: 'Exception occurred. \r\n')

I found this question . 我发现了这个问题 In it, the error number is the same, but their problem is the wrong sheet name. 在其中,错误号是相同的,但是他们的问题是工作表名称错误。 My sheet name, 'abc' really does exist in the excel file. 我的工作表名称“ abc”确实存在于excel文件中。

The following might also be useful diagnostic information. 以下内容也可能是有用的诊断信息。 This code works (to create and open a new workbook): 此代码有效(用于创建和打开新工作簿):

def make_xl():
    o = win32com.client.Dispatch("Excel.Application")
    o.Visible = 1
    o.Workbooks.Add() # for office 97 – 95 a bit different!
    o.Cells(1,1).Value = "Hello"

Other info: Windows 10 Python 3.6 Python win is installed (pywin32 build 222) 其他信息:已安装Windows 10 Python 3.6 Python win(pywin32内部版本222)

I am new to using win32com. 我是使用win32com的新手。 Please help me troubleshoot this problem. 请帮助我解决此问题。

I think your problem comes from: 我认为您的问题来自:

wb = excel.Workbooks(r'C:\Users\<username>\Documents\<more file path>\test.xls').Open()

The path to your file should be in Open() such as: 文件的路径应位于Open()例如:

wb = excel.Workbooks.Open(r'C:\Users\<username>\Documents\<more file path>\test.xls')

Note also that there is no () after Workbooks as in your second code. 还要注意, Workbooks后没有(),就像第二个代码一样。 This way works for me. 这种方式对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM