使用带有python

时间:2016-02-25 14:24:34

标签: python

我正在尝试使用Python 3.4学习pdfcrowd,所以我查看了他们的网站并复制了以下示例:

import pdfcrowd

try:
    # create an API client instance
    client = pdfcrowd.Client("username", "apikey")

    # convert a web page and store the generated PDF into a pdf variable
    pdf = client.convertURI('http://www.google.com')

    # convert an HTML string and save the result to a file
    output_file = open('html.pdf', 'wb')
    html="<head></head><body>My HTML Layout</body>"
    client.convertHtml(html, output_file)
    output_file.close()

    # convert an HTML file
    output_file = open('file.pdf', 'wb')
    client.convertFile('/path/to/MyLayout.html', output_file)
    output_file.close()

except pdfcrowd.Error, why:
    print('Failed: {}'.format(why))

当我尝试运行它时,我收到以下错误:

File "pf.py" line 21
   except pdfcrowd.Error, why:
                        ^
SyntaxError: invalid syntax

有谁能告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

这意味着将错误分配给变量why。这是Python2中的有效语法,但不适用于Python3。请改用except pdfcrowd.Error as why:。这是Python2 Python3

中的有效语法