TypeError: 'NotImplementedType'对象不可迭代
我正在尝试使用xhtml2pdf从HTML页面创建一个PDF文件。在转换过程中,我的终端显示了以下错误信息。
错误信息“TypeError: 'NotImplementedType' object is not iterable”是什么意思呢?
我该如何避免这个错误?以下是我用来转换PDF的示例代码……
<html>
<head>
<style type="text/css">
@page
{
@top-right
{
content: "Page " counter(page);
}
}
</style>
</head>
<body>
test
</body>
</html>
我还使用了以下命令行进行PDF转换……
xhtml2pdf test.html test.pdf
在这个转换过程中,我遇到了之前提到的错误。我该怎么办呢?
1 个回答
1
这看起来是xhtml2pdf里的一个bug。
当我运行你的命令时,出现的错误信息如下:
Traceback (most recent call last):
File "C:\Python27\Scripts\xhtml2pdf-script.py", line 9, in <module>
load_entry_point('xhtml2pdf==0.0.6', 'console_scripts', 'xhtml2pdf')()
File "build\bdist.win32\egg\xhtml2pdf\pisa.py", line 146, in command
File "build\bdist.win32\egg\xhtml2pdf\pisa.py", line 363, in execute
File "build\bdist.win32\egg\xhtml2pdf\document.py", line 89, in pisaDocument
File "build\bdist.win32\egg\xhtml2pdf\document.py", line 57, in pisaStory
File "build\bdist.win32\egg\xhtml2pdf\parser.py", line 685, in pisaParser
File "build\bdist.win32\egg\xhtml2pdf\context.py", line 498, in parseCSS
File "build\bdist.win32\egg\xhtml2pdf\w3c\cssParser.py", line 434, in parse
File "build\bdist.win32\egg\xhtml2pdf\w3c\cssParser.py", line 533, in _parseStylesheet
File "build\bdist.win32\egg\xhtml2pdf\w3c\cssParser.py", line 654, in _parseAtKeyword
File "build\bdist.win32\egg\xhtml2pdf\w3c\cssParser.py", line 758, in _parseAtPage
TypeError: 'NotImplementedType' object is not iterable
深入看看代码,问题似乎出现在这里,也就是在 w3c/cssParser.py
文件中。
if src.startswith('@'):
# @media, @page, @font-face
src, atResults = self._parseAtKeyword(src)
if atResults is not None:
stylesheetElements.extend(atResults) # this is line 758
_parseAtKeyword(src)
这个函数会返回一个包含 src
和 NotImplemented
的二元组,对于它不认识的任何CSS关键字,尤其是 @top-right
。这就导致了下一行的 extend
失败,因为 NotImplemented
不是一个有效的 extend
参数。
我对 @top-right
不太熟悉,谷歌搜索 css @top-right
也没有找到相关结果。如果我把 @top-right
替换成 @topright
,结果还是一样;而如果我把 @top-right
替换成 top-right
,代码就进入了无限循环。所以我对xhtml2pdf的CSS解析器并不太满意。
把第757行替换成
if atResults is not None and atResults is not NotImplemented:
似乎可以让xhtml2pdf正常工作,能够把你的测试文档转换成PDF。不过,它生成的输出可能不是你期望的那样。
我觉得最好的办法是去 xhtml2pdf的邮件列表 上发帖询问。