python集成到excel使用pyxll …有问题的lxml模块

我是python的新手。 我试图从互联网上得到一个词的意思。 独立的Python代码工作得很好。

from lxml import html import requests url = "http://dictionnaire.reverso.net/francais-definition/" word = raw_input("please enter the word you want to translate ") url = url + word page = requests.get(url) tree= html.fromstring(page.text) translation = tree.xpath('//*[@id="ID0EYB"]/text()') print translation 

请注意,我正在使用的xpath仅用于testing目的。 用“manger”,“gonfler”等简单的单词很好地工作。我尝试的下一步是使用pyxll插件为excel在excel中创build一个function为同一个任务。

  from pyxll import xl_func from lxml import html import requests @xl_func("string x: string") def traduction(x): url = "http://dictionnaire.reverso.net/francais-definition/" url = url + x page = requests.get(url) tree= html.fromstring(page.text) translation = tree.xpath('//*[@id="ID0EYB"]/text()') return translation 

之后,当我开始excel,我得到一个错误。 在pyxll的日志文件中,错误描述如下:

  2014-09-09 17:02:41,845 - ERROR : Error importing 'worksheetfuncs': DLL load failed: Le module spécifié est introuvable. 2014-09-09 17:02:41,845 - ERROR : Traceback (most recent call last): 2014-09-09 17:02:41,845 - ERROR : File "pyxll", line 791, in _open 2014-09-09 17:02:41,845 - ERROR : File "\pyxll\examples\worksheetfuncs.py", line 317, in <module> 2014-09-09 17:02:41,845 - ERROR : from lxml import html 2014-09-09 17:02:41,846 - ERROR : File "C:\Python27\lib\site-packages\lxml\html\__init__.py", line 42, in <module> 2014-09-09 17:02:41,846 - ERROR : from lxml import etree 2014-09-09 17:02:41,846 - ERROR : ImportError: DLL load failed: Le module spécifié est introuvable. 2014-09-09 17:02:41,888 - WARNING : pydevd failed to import - eclipse debugging won't work 2014-09-09 17:02:41,888 - WARNING : Check the eclipse path in \pyxll\examples\tools\eclipse_debug.pyc 2014-09-09 17:02:41,890 - INFO : callbacks.license_notifier: This copy of PyXLL is for evaluation or non-commercial use only 

我已经使用API​​的翻译网站做类似的东西,它工作正常。 对我来说真正的问题是我使用lxmlparsing,似乎lxml和pyxll不走在一起。 请帮助!!!

你有没有安装pywin32 ? Python需要与Windows应用程序(如Excel)交互。

我切换到urllib2和beautifulsoup这与pyxll运作良好。 下面是excel中数组函数的工作代码,它包含多个单词并给出两个含义。 但是,我使用的CSSselect过于严格,我还没有在网站上find任何可以用来获取任何单词意义的模式。

 from pyxll import xl_func import urllib2 from bs4 import BeautifulSoup @xl_func("var[] x: var[]") def dictionnaire(x): height = len(x) meanings = [] for i in range(height): word = x[i][0] row = [] url = "http://dictionnaire.reverso.net/francais-definition/" url = url + word page = urllib2.urlopen(url) soup = BeautifulSoup(page) results = soup.select("#ID0ENC") row.append(results[0].get_text()) row.append(results[1].get_text()) meanings.append(row) return meanings