【发布时间】:2019-04-18 13:31:43
【问题描述】:
我想创建一个带有指向另一个脚本的超链接的 ToC,并想了解它是如何工作的,但我无法让它与我找到的这个示例一起工作。你可以帮帮我吗?我刚刚收到错误消息:
Unicode 对象必须在散列之前进行编码
这是整个例子: https://www.reportlab.com/snippets/13/
#this function makes our headings
def doHeading(text,sty):
from hashlib import sha1
#create bookmarkname
bn=sha1(text+sty.name).hexdigest()
#modify paragraph text to include an anchor point with name bn
h=Paragraph(text+'<a name="%s"/>' % bn,sty)
#store the bookmark name on the flowable so afterFlowable can see this
h._bookmarkName=bn
story.append(h)
story.append(Paragraph('<b>Table of contents</b>', centered))
story.append(PageBreak())
doHeading('First heading', h1)
story.append(Paragraph('Text in first heading', PS('body')))
doHeading('First sub heading', h2)
story.append(Paragraph('Text in first sub heading', PS('body')))
story.append(PageBreak())
doHeading('Second sub heading', h2)
story.append(Paragraph('Text in second sub heading', PS('body')))
story.append(PageBreak())
doHeading('Last heading', h1)
story.append(Paragraph('Text in last heading', PS('body')))
doc = MyDocTemplate('mintoc.pdf')
doc.multiBuild(story)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-16-138d578aa6aa> in <module>
83 story.append(Paragraph('<b>Table of contents</b>', centered))
84 story.append(PageBreak())
---> 85 doHeading('First heading', h1)
86 story.append(Paragraph('Text in first heading', PS('body')))
87 doHeading('First sub heading', h2)
<ipython-input-16-138d578aa6aa> in doHeading(text, sty)
74 from hashlib import sha1
75 #create bookmarkname
---> 76 bn=sha1(text+sty.name).hexdigest()
77 #modify paragraph text to include an anchor point with name bn
78 h=Paragraph(text+'<a name="%s"/>' % bn,sty)
TypeError: Unicode-objects must be encoded before hashing
【问题讨论】:
-
您能否分享显示错误行的完整跟踪?
标签: python unicode reportlab tableofcontents