Python论坛  - 讨论区

标题:[python-chinese] 回复: PythonWin结合excel 2003使用,打开xls文件,Workbooks.Open方法报错,这是怎么回事呢?

2007年07月13日 星期五 13:41

汪文进 wjawon在yahoo.com.cn
星期五 七月 13 13:41:24 HKT 2007

我修改了源碼,你可以很清楚地看出原因:
01. import os,sys
02. import win32com.client
03. import pythoncom
04. 
05. print 'run path:\t',os.getcwd()
06. print 'scipt path:\t', sys.argv[0]
07. print
08. 
09. 
10. class easyExcel:
11.     def __init__(self,filename):
12.         try:
13.             self.xlApp = win32com.client.dynamic.Dispatch('Excel.Application')
14.             self.filename = filename
15.             self.xlBook = self.xlApp.Workbooks.Open(filename)
16.         except pythoncom.com_error, (hr, msg, exc, arg):
17.             print 'hr:\t',hr
18.             print 'msg:\t',msg
19.             print 'exc[0]:\t',exc[0]
20.             print 'exc[1]:\t',exc[1]
21.             print 'exc[2]:\t',exc[2]
22.             print 'exc[3]:\t',exc[3]
23.             print 'arg:\t',arg
24. 
25. 
26. spr = easyExcel('Book2.xls')
27. raw_input()

你可以用'Book2.xls'文件的絕對路徑,或在easyExcel.__init__中處理,如os.getcwd()或sys.argv[0]




----- 原始邮件 ----
发件人: 张建轶 <farsoftwind在gmail.com>
收件人: python-chinese在lists.python.cn
已发送: 2007/7/12(周四), 下午7:36:42
主题: [python-chinese] PythonWin结合excel 2003使用,打开xls文件,Workbooks.Open方法报错,这是怎么回事呢?

使用PythonWin调用Excel 的 COM 组件,部分程序如下,但程序应该没有问题,因为是节选自一本很经典的书

class easyExcel:
   def __init__(self,filename):
        self.xlApp = win32com.client.dynamic.Dispatch('Excel.Application')
        self.filename = filename
        self.xlBook = self.xlApp.Workbooks.Open(filename)


spr = easyExcel('Book2.xls')

运行报错如下
  File "D:\mytemp\ch09_excel\exceldemos.py", line 155, in getInvoices
    spr = easyExcel('Book2.xls')
  File "D:\mytemp\ch09_excel\exceldemos.py", line 30, in __init__
    self.xlBook = self.xlApp.Workbooks.Open(filename)
  File ">", line 8, in Open 
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Office Excel', '\xce\xde\xb7\xa8\xd5\xd2\xb5\xbd\xa1\xb0Book2.xls\xa1\xb1\xa1\xa3\xc7\xeb\xbc\xec\xb2\xe9\xce\xc4\xbc\xfe\xc3\xfb\xb5\xc4\xc6\xb4\xd0\xb4\xa3\xac\xb2\xa2\xbc\xec\xb2\xe9\xce\xc4\xbc\xfe\xce\xbb\xd6\xc3\xca\xc7\xb7\xf1\xd5\xfd\xc8\xb7\xa1\xa3\n\n\xc8\xe7\xb9\xfb\xc4\xfa\xd5\xfd\xca\xd4\xcd\xbc\xb4\xd3\xa1\xb0\xce\xc4\xbc\xfe\xa1\xb1\xb2\xcb\xb5\xa5\xc9\xcf\xd7\xee\xbd\xfc\xca\xb9\xd3\xc3\xb5\xc4\xce\xc4\xbc\xfe\xc1\xd0\xb1\xed\xd6\xd0\xb4\xf2\xbf\xaa\xce\xc4\xbc\xfe\xa3\xac\xc7\xeb\xc8\xb7\xc8\xcf\xce\xc4\xbc\xfe\xce\xb4\xb1\xbb\xd6\xd8\xc3\xfc\xc3\xfb\xa1\xa2\xd2\xc6\xb6\xaf\xbb\xf2\xc9\xbe\xb3\xfd\xa1\xa3', 'C:\\Program Files\\Microsoft Office\\OFFICE11\\2052\\xlmain11.chm', 0, -2146827284), None) 

请教该如何解决?




_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese




	
		
___________________________________________________________ 
Mp3疯狂搜-新歌热歌高速下
http://music.yahoo.com.cn/?source=mail_mailbox_footer
-------------- 涓嬩竴閮ㄥ垎 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20070713/55cb3ab8/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月13日 星期五 14:40

cun heise cunheise在hotmail.com
星期五 七月 13 14:40:01 HKT 2007

ÓÃpyExcel£¬ºÃÏñÕâ¸öÃû×Ö£¬opensourceµÄ


>From: ÍôÎĽø <wjawon在yahoo.com.cn>
>Reply-To: python-chinese在lists.python.cn
>To: python-chinese在lists.python.cn
>Subject: [python-chinese] »Ø¸´£º  PythonWin½áºÏexcel 2003ʹÓ㬴ò¿ªxlsÎļþ
£¬Workbooks.Open·½·¨±¨´í£¬ÕâÊÇÔõô»ØÊÂÄØ£¿
>Date: Fri, 13 Jul 2007 13:41:24 +0800 (CST)
>
>ÎÒÐÞ¸ÄÁËÔ´´a,Äã¿ÉÒÔºÜÇå³þµØ¿´³öÔ­Òò:
>01. import os,sys
>02. import win32com.client
>03. import pythoncom
>04.
>05. print 'run path:\t',os.getcwd()
>06. print 'scipt path:\t', sys.argv[0]
>07. print
>08.
>09.
>10. class easyExcel:
>11.     def __init__(self,filename):
>12.         try:
>13.             self.xlApp = 
win32com.client.dynamic.Dispatch('Excel.Application')
>14.             self.filename = filename
>15.             self.xlBook = self.xlApp.Workbooks.Open(filename)
>16.         except pythoncom.com_error, (hr, msg, exc, arg):
>17.             print 'hr:\t',hr
>18.             print 'msg:\t',msg
>19.             print 'exc[0]:\t',exc[0]
>20.             print 'exc[1]:\t',exc[1]
>21.             print 'exc[2]:\t',exc[2]
>22.             print 'exc[3]:\t',exc[3]
>23.             print 'arg:\t',arg
>24.
>25.
>26. spr = easyExcel('Book2.xls')
>27. raw_input()
>
>Äã¿ÉÒÔÓÃ'Book2.xls'ÎļþµÄ½^Œ¦Â·½,»òÔÚeasyExcel.__init__ÖÐ̎Àí,Èç
os.getcwd()»òsys.argv[0]
>
>
>
>
>----- ԭʼÓʼþ ----
>·¢¼þÈË£º ÕŽ¨éó <farsoftwind在gmail.com>
>ÊÕ¼þÈË£º python-chinese在lists.python.cn
>ÒÑ·¢ËÍ£º 2007/7/12(ÖÜËÄ), ÏÂÎç7:36:42
>Ö÷Ì⣺ [python-chinese] PythonWin½áºÏexcel 2003ʹÓ㬴ò¿ªxlsÎļþ£¬
Workbooks.Open·½·¨±¨´í£¬ÕâÊÇÔõô»ØÊÂÄØ£¿
>
>ʹÓÃPythonWinµ÷ÓÃExcel µÄ COM ×é¼þ£¬²¿·Ö³ÌÐòÈçÏ£¬µ«³ÌÐòÓ¦¸ÃûÓÐÎÊÌ⣬ÒòΪ
ÊǽÚÑ¡×ÔÒ»±¾ºÜ¾­µäµÄÊé
>
>class easyExcel:
>    def __init__(self,filename):
>         self.xlApp = 
win32com.client.dynamic.Dispatch('Excel.Application')
>         self.filename = filename
>         self.xlBook = self.xlApp.Workbooks.Open(filename)
>
>
>spr = easyExcel('Book2.xls')
>
>ÔËÐб¨´íÈçÏÂ
>   File "D:\mytemp\ch09_excel\exceldemos.py", line 155, in getInvoices
>     spr = easyExcel('Book2.xls')
>   File "D:\mytemp\ch09_excel\exceldemos.py", line 30, in __init__
>     self.xlBook = self.xlApp.Workbooks.Open(filename)
>   File ">", line 8, in Open
>com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Office 
Excel', 
'\xce\xde\xb7\xa8\xd5\xd2\xb5\xbd\xa1\xb0Book2.xls\xa1\xb1\xa1\xa3\xc7\xeb\xbc\xec\xb2\xe9\xce\xc4\xbc\xfe\xc3\xfb\xb5\xc4\xc6\xb4\xd0\xb4\xa3\xac\xb2\xa2\xbc\xec\xb2\xe9\xce\xc4\xbc\xfe\xce\xbb\xd6\xc3\xca\xc7\xb7\xf1\xd5\xfd\xc8\xb7\xa1\xa3\n\n\xc8\xe7\xb9\xfb\xc4\xfa\xd5\xfd\xca\xd4\xcd\xbc\xb4\xd3\xa1\xb0\xce\xc4\xbc\xfe\xa1\xb1\xb2\xcb\xb5\xa5\xc9\xcf\xd7\xee\xbd\xfc\xca\xb9\xd3\xc3\xb5\xc4\xce\xc4\xbc\xfe\xc1\xd0\xb1\xed\xd6\xd0\xb4\xf2\xbf\xaa\xce\xc4\xbc\xfe\xa3\xac\xc7\xeb\xc8\xb7\xc8\xcf\xce\xc4\xbc\xfe\xce\xb4\xb1\xbb\xd6\xd8\xc3\xfc\xc3\xfb\xa1\xa2\xd2\xc6\xb6\xaf\xbb\xf2\xc9\xbe\xb3\xfd\xa1\xa3', 
'C:\\Program Files\\Microsoft Office\\OFFICE11\\2052\\xlmain11.chm', 0, 
-2146827284), None)
>
>Çë½Ì¸ÃÈçºÎ½â¾ö£¿
>
>
>
>
>_______________________________________________
>python-chinese
>Post: send python-chinese在lists.python.cn
>Subscribe: send subscribe to python-chinese-request在lists.python.cn
>Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
>Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
>
>
>
>___________________________________________________________
>Mp3·è¿ñËÑ-иèÈȸè¸ßËÙÏÂ
>http://music.yahoo.com.cn/?source=mail_mailbox_footer


>_______________________________________________
>python-chinese
>Post: send python-chinese在lists.python.cn
>Subscribe: send subscribe to python-chinese-request在lists.python.cn
>Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
>Detail Info: http://python.cn/mailman/listinfo/python-chinese

_________________________________________________________________
ÓëÁª»úµÄÅóÓѽøÐн»Á÷£¬ÇëʹÓà Live Messenger; 
http://get.live.com/messenger/overview 


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号