最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中出错:pywintypes.com_error,Exception occurred,Microsoft Excel,could not be found

Python crifan 19846浏览 0评论

【问题】

写了代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【已解决】Python中处理操作Excel中的图表(Chart,Graph)
https://www.crifan.com/python_process_excel_chart_graph

Author:     Crifan Li
Version:    2012-12-25
Contact:    admin at crifan dot com
"""

from win32com.client import Dispatch;
#from win32com.client import *;

def excelChart():
    xl = Dispatch("Excel.Application");
    #xl = win32com.client.Dispatch("Excel.Application")
    print "xl=",xl;

    wb = xl.Workbooks.open("chart_demo.xls");
    xl.Visible = 1;
    ws = wb.Worksheets(1);
    ws.Range('$A1:$D1').Value = ['NAME', 'PLACE', 'RANK', 'PRICE'];
    ws.Range('$A2:$D2').Value = ['Foo', 'Fooland', 1, 100];
    ws.Range('$A3:$D3').Value = ['Bar', 'Barland', 2, 75];
    ws.Range('$A4:$D4').Value = ['Stuff', 'Stuffland', 3, 50];
    wb.Save();
    wb.Charts.Add();
    wc1 = wb.Charts(1);
    
if __name__ == "__main__":
    excelChart();

运行出错:

D:\tmp\tmp_dev_root\python\excel_chart>excel_chart.py

xl= Microsoft Excel

Traceback (most recent call last):

  File "D:\tmp\tmp_dev_root\python\excel_chart\excel_chart.py", line 34, in <module>

    excelChart();

  File "D:\tmp\tmp_dev_root\python\excel_chart\excel_chart.py", line 21, in excelChart

    wb = xl.Workbooks.open("chart_demo.xls");

  File "<COMObject <unknown>>", line 2, in open

pywintypes.com_error: (-2147352567, ‘Exception occurred.’, (0, u’Microsoft Excel’, u"’chart_demo.xls’ could not be found. Check the spelling of the file name, and verify that the file location is corr

ect.\n\nIf you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.", u’xlmain11.chm’, 0, -2146827284), None)

D:\tmp\tmp_dev_root\python\excel_chart>D:\tmp\tmp_dev_root\python\excel_chart\chart_demo.xls

但是此处的确已经确保了,存在了chart_demo.xls文件的。

 

【解决过程】

1.改为:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【已解决】Python中处理操作Excel中的图表(Chart,Graph)
https://www.crifan.com/python_process_excel_chart_graph

Author:     Crifan Li
Version:    2012-12-25
Contact:    admin at crifan dot com
"""

from win32com.client import Dispatch;
#from win32com.client import *;

def excelChart():
    xl = Dispatch("Excel.Application");
    #xl = win32com.client.Dispatch("Excel.Application")
    print "xl=",xl;

    #wb = xl.Workbooks.open("chart_demo.xls");
    wb = xl.Workbooks.open("D:\tmp\tmp_dev_root\python\excel_chart\chart_demo.xls");
    xl.Visible = 1;
    ws = wb.Worksheets(1);
    ws.Range('$A1:$D1').Value = ['NAME', 'PLACE', 'RANK', 'PRICE'];
    ws.Range('$A2:$D2').Value = ['Foo', 'Fooland', 1, 100];
    ws.Range('$A3:$D3').Value = ['Bar', 'Barland', 2, 75];
    ws.Range('$A4:$D4').Value = ['Stuff', 'Stuffland', 3, 50];
    wb.Save();
    wb.Charts.Add();
    wc1 = wb.Charts(1);
    
if __name__ == "__main__":
    excelChart();

结果错误依旧。

2。参考:

‘Run Excel File From Python’ Error

去改为:

wb = xl.Workbooks.open(r"D:\tmp\tmp_dev_root\python\excel_chart\chart_demo.xls");

结果就可以正常打开了:

can show chart

 

【总结】

正如某人所说,Excel中的路径,和Python中所能自动处理的操作系统中文件夹的路径,是不一样。

其主要是无法自动识别反斜杠。

而把原先的字符串,添加个r前缀,表述raw的string后,就可以自动识别了。

 

最后,又去整理了下,试了各种组合,最终结果是:

    #[1] Fail
    # xlsPath = "chart_demo.xls";
    # wb = xl.Workbooks.open(xlsPath); #pywintypes.com_error

    #[2] Fail
    # xlsPath = "D:\tmp\tmp_dev_root\python\excel_chart\chart_demo.xls";
    # absPath = os.path.abspath(xlsPath);
    # print "absPath=",absPath; #absPath= D:\tmp\tmp_dev_root\python\excel_chart\        mp      mp_dev_root\python\excel_chart\chart_demo.xls
    # wb = xl.Workbooks.open(absPath); #pywintypes.com_error

    #[3] Fail
    # xlsPath = "D:\tmp\tmp_dev_root\python\excel_chart\chart_demo.xls";
    # normalPath = os.path.normpath(xlsPath);
    # print "normalPath=",normalPath; #normalPath= D:  mp      mp_dev_root\python\excel_chart\chart_demo.xls
    # wb = xl.Workbooks.open(normalPath); #pywintypes.com_error

    #[4] Fail
    # rawPath = r"chart_demo.xls";
    # wb = xl.Workbooks.open(rawPath); #pywintypes.com_error
    
    #[5] OK
    xlsPath = "chart_demo.xls";
    absPath = os.path.abspath(xlsPath);
    print "absPath=",absPath; #absPath= D:\tmp\tmp_dev_root\python\excel_chart\chart_demo.xls
    wb = xl.Workbooks.open(absPath); #OK
        
    #[6] OK
    # rawPath = r"D:\tmp\tmp_dev_root\python\excel_chart\chart_demo.xls";
    # wb = xl.Workbooks.open(rawPath); # OK

转载请注明:在路上 » 【已解决】Python中出错:pywintypes.com_error,Exception occurred,Microsoft Excel,could not be found

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.216 seconds, using 22.14MB memory