reportlab TableOfContent 目录的构建

构建目录,需要重写一个类,他的父类就是BaseDocTemplate类,之后重写你创建的类的afterFlowable方法。有两个官网给的样例。

样例一:生成目录,没有bookmark。https://www.reportlab.com/snippets/8/

样例二:生成目录,并且有bookmark,点击目录会跳转到点击目录的页面。https://www.reportlab.com/snippets/13/

根据样例一和样例二,做了一点小小的改变,由于0级目录没有名称后面的..........,所以我把我的所有的本来应该放0级目录的地方变为一级目录,这样目录后面就会有......了。

def afterFlowable(self, flowable):
        "Registers TOC entries."
        if flowable.__class__.__name__ == 'Paragraph':
            text = flowable.getPlainText()
            style = flowable.style.name
            if style == 'Heading1':
                level = 1
            else:
                return
            E = [level, text, self.page]
            bn = getattr(flowable, '_bookmarkName', None)                                                 #得到bookmark的属性值
            if bn is not None:
                E.append(bn)
            self.notify('TOCEntry', tuple(E))                                                             #将此信息放入目录中

在之后的构建toc中,要两个level的格式都写上,虽然只使用了一个,但是只写一个好像不可以。

toc = tableofcontents.TableOfContents()                               #声明目录结构类
toc.levelStyles = [
    ParagraphStyle(fontName='STSong-Light', fontSize=20, name='TOCHeading1', leftIndent = 20, firstLineIndent = -20, spaceBefore=10, leading = 16),
    ParagraphStyle(fontName='STSong-Light', fontSize=18, name='TOCHeading2', leftIndent = 30, firstLineIndent = -20, spaceBefore = 5, leading = 12)
]                                                                     #设置目录结构字体
text.append(toc)                                                      #将目录放入text中
text.append(PageBreak())                                              #换页符

这样应该就可以了,在最后的时候要使用doc.multiBuild()这个函数,build()这个函数好像对TableOfContent不起作用,具体在头部import什么库文件,可以参考样例一,和样例二。

有什么错误的地方,希望大家指正,谢谢大家!

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
ReportLab is a powerful Python library for creating PDF documents with various elements such as text, images, and tables. In this case, we will focus on creating tables using ReportLab. To create a table in ReportLab, we first need to import the necessary packages: ``` from reportlab.lib.pagesizes import letter from reportlab.lib import colors from reportlab.lib.styles import getSampleStyleSheet from reportlab.platypus import SimpleDocTemplate, Table, TableStyle ``` Next, we can define the data to be included in the table. This can be done using a nested list, where each sublist represents a row in the table: ``` data = [ ["Name", "Age", "Gender"], ["John", 25, "Male"], ["Jane", 30, "Female"], ["Bob", 40, "Male"], ["Mary", 35, "Female"] ] ``` We can then create a table object using this data: ``` table = Table(data) ``` To add styling to the table, we can define a TableStyle object and apply it to the table: ``` style = TableStyle([ ('BACKGROUND', (0,0), (-1,0), colors.grey), ('TEXTCOLOR', (0,0), (-1,0), colors.whitesmoke), ('ALIGN', (0,0), (-1,-1), 'CENTER'), ('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'), ('FONTSIZE', (0,0), (-1,0), 14), ('BOTTOMPADDING', (0,0), (-1,0), 12), ('BACKGROUND', (0,1), (-1,-1), colors.beige), ('TEXTCOLOR', (0,1), (-1,-1), colors.black), ('ALIGN', (-1,0), (-1,-1), 'CENTER'), ('FONTNAME', (0,1), (-1,-1), 'Helvetica'), ('FONTSIZE', (0,1), (-1,-1), 12), ('BOTTOMPADDING', (0,1), (-1,-1), 6), ('VALIGN', (0,0), (-1,-1), 'MIDDLE'), ]) table.setStyle(style) ``` Finally, we can include the table in our PDF document: ``` pdf_doc = SimpleDocTemplate("my_table.pdf", pagesize=letter) pdf_doc.build([table]) ``` This will create a PDF document with a table that has styling applied to it. You can adjust the styling to fit your needs.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值