2012-04-30 141 views
2

我想创建一个带有pyvot的excel工作簿,如this tutorial所示。 Pyvot安装正确,因为我可以导入它没有错误。但是,当我做的:xl.Workbook()(pyvot)无法打开excel工作簿

import xl 
xl.Workbook() 

我得到这个错误:

Traceback (most recent call last): 
    File "", line 1, in 
AttributeError: 'module' object has no attribute 'workbook' 
>>> xl.Workbook() 
Traceback (most recent call last): 
    File "", line 1, in 
    File "C:\Python27\lib\site-packages\pyvot-0.1.2-py2.7.egg\xl\sheet.py", line 91, in  __init__ 
    excel = win32.gencache.EnsureDispatch('Excel.Application') 
    File "C:\Python27\lib\site-packages\win32com\client\gencache.py", line 529, in EnsureDispatch 
    disp = win32com.client.Dispatch(prog_id) 
    File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch 
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx) 
    File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 108, in _GetGoodDispatchAndUserName 
    return (_GetGoodDispatch(IDispatch, clsctx), userName) 
    File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 85, in _GetGoodDispatch 
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) 
com_error: (-2147221005, 'Invalid class string', None, None) 

我没有Python的工具,虽然安装了Visual Studio。

  1. 我是否需要用于visual studio(PTVS)的Python工具才能使用pyvot?

  2. 可以降低版本的Excel然后Excel 2010(例如Excel 2007)与pyvot一起工作?

  3. 我是否需要在excel中设置任何东西以使其与pyvot一起工作?

如果其中一个问题的答案上面没有解释为什么我不能作出这样的代码片段,我应该怎样做才能得到它正常工作?


我已经安装了Excel 2010中Pyvot正确安装,因为我当我做import xl没有得到任何错误。当我输入pywin32如下:

import xl 
xl.Workbook() 

我得到这个NameError:

Traceback (most recent call last): 
    File "<interactive input>", line 1, in <module> 
NameError: name 'xl' is not defined 

有谁知道为什么吗?

回答

2

Pyvot主页:

Pyvot requires CPython 2.6 or 2.7 with the Python for Windows extensions (pywin32) installed, and Office 2010. If a clean Python session can import the win32com module, Pyvot is ready to be installed.

所以从它的描述,需要不需要PTVS,Excel 2010中的,你需要win32com模块并在当前的解释运行。我使用这个配置没有任何问题。

+0

我已经安装了Excel 2010,但我仍然得到一个NameError(请参阅更新的原始文章),任何线索为什么? – Bentley4

+0

你确定你的'import xl'语句工作正常吗?你的错误很可能来自'xl.Workbook()',当xl未知时 – Boud

+0

'import xl'正常工作。 – Bentley4

1

事情发生在我身上,我今天就试了一下。
尝试: xl.workbooks()

这与VBA代码一致。

相关问题