“从Python运行Excel文件"错误 [英] 'Run Excel File From Python' Error

查看:75
本文介绍了“从Python运行Excel文件"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试通过从python调用EXCEL本身打开Excel文件时,出现错误.我该如何解决?

When I try to open an excel file by calling EXCEL itself from python, I get error. How can I fix that?

谢谢.

代码是:

    from win32com.client import Dispatch
    xl = Dispatch('Excel.Application')
    wb = xl.Workbooks.Open(r"data\Modules.xls")       

错误是:

pywintypes.com_error:(-2147352567,'发生了异常.',(0,找不到'Microsoft Office Excel','"data \ Modules.xls'.请检查文件名的拼写,以及验证文件位置是否正确.\ n \ n如果您尝试从最近使用过的文件列表中打开文件,请确保未重命名,移动或删除该文件.,u'C:\程序文件(x86)\ Microsoft Office \ Office12 \ 1033 \ XLMAIN11.CHM',0,-2146827284),无)

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u"'data\Modules.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.\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'C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM', 0, -2146827284), None)

推荐答案

使用os.path.abspath()将文件系统路径转换为绝对路径.您的Python和Excel进程的当前工作目录不相同.

Use os.path.abspath() to convert file system paths to absolute. The current working directory of yout Python and Excel process is not the same.

http://docs.python.org/library/os.path.html

这篇关于“从Python运行Excel文件"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆