【问题标题】:integration of python into excel using pyxll... having problems with lxml module使用pyxll将python集成到excel中...... lxml模块有问题
【发布时间】:2014-09-09 15:45:08
【问题描述】:

我是 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 仅用于测试目的。与“manger”、“gonfler”等简单的词配合使用效果很好。 我正在尝试的下一步是使用 pyxll addin for excel 在 excel 中为同一任务创建一个函数。

   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 的翻译网站来做类似的事情,而且效果很好。对我来说,真正的问题是我使用 lxml 进行解析,似乎 lxml 和 pyxll 不能一起使用。请帮忙!!!

【问题讨论】:

  • 请修正示例代码中的缩进。

标签: python xml excel lxml pyxll


【解决方案1】:

你安装pywin32了吗? Python 需要它来与 Excel 等 Windows 应用程序交互。

【讨论】:

  • 嗨...我已经安装了 pywin32。它仍然不起作用。我已经更新了错误。
  • 引用自pyxll forum“我希望它无法找到 msvcrt90.dll。(...) 请您检查一下是否有它?(可能在 C:\Python27 ( www.dependencywalker.com)在有问题的 .pyd 文件上检查它缺少哪些 dll 依赖项。”我猜你使用的是 Python34,但想法是一样的
  • 忽略 Python34 参考。我错过了你在错误输出中使用的是 Python27。
【解决方案2】:

我切换到 urllib2 和 beautifulsoup,它们与 pyxll 配合得很好。下面是excel中数组函数的工作代码,它接受多个单词并给出两种含义。但是,我使用的 CSS 选择过于严格,我还没有在网站中找到可以用来获取任何单词含义的模式。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-20
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多