Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用链接[Python&x27;的FPDF包]创建交互式目录的问题_Python_Pdf_Fpdf_Pypdf2_Tableofcontents - Fatal编程技术网

使用链接[Python&x27;的FPDF包]创建交互式目录的问题

使用链接[Python&x27;的FPDF包]创建交互式目录的问题,python,pdf,fpdf,pypdf2,tableofcontents,Python,Pdf,Fpdf,Pypdf2,Tableofcontents,我正在尝试使用Python的FPDF包创建目录 我希望目录位于PDF文档的顶部,并可单击,以便读者立即进入某个部分 我遇到的问题是,要使链接起作用,目录必须位于文档的末尾。 我曾尝试使用python的PyPDF2分割创建的PDF文档,并将目录移动到文档的开头。问题是,在生成的PDF中,链接不再起作用 我将python(v3.7)与Spyder一起使用 以下是我创建目录的代码: from fpdf import FPDF # This is my section with link to tab

我正在尝试使用Python的FPDF包创建目录

我希望目录位于PDF文档的顶部,并可单击,以便读者立即进入某个部分

我遇到的问题是,要使链接起作用,目录必须位于文档的末尾。

我曾尝试使用python的PyPDF2分割创建的PDF文档,并将目录移动到文档的开头。问题是,在生成的PDF中,链接不再起作用

我将python(v3.7)与Spyder一起使用

以下是我创建目录的代码:

from fpdf import FPDF

# This is my section with link to table of contents
report.add_page(orientation='p')
report.set_font('Helvetica', size=22, style ='B')
report.cell(200, 10, 'section', 0, 2, 'l') 
DD = report.add_link()
report.set_link(DD)

# Table of contents including that one section
report.write(5, 'Section',DD) 
from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger

PDF_report = PdfFileReader('file.pdf')
pdf_bulk_writer = PdfFileWriter()
output_filename_bulk = "bulk.pdf"
pdf_TOC_writer = PdfFileWriter()

output_filename_TOC = "TOC.pdf"

for page in range(PDF_report.getNumPages()):
    current_page = PDF_report.getPage(page)
    if page == PDF_report.getNumPages()-1:
        pdf_TOC_writer.addPage(current_page)
    if page <= PDF_report.getNumPages()-2:
        pdf_bulk_writer.addPage(current_page)

# Write the data to disk
with open(output_filename_TOC, "wb") as out:
     pdf_TOC_writer.write(out)
     print("created", output_filename_TOC)       

# Write the data to disk
with open(output_filename_bulk, "wb") as out:
     pdf_bulk_writer.write(out)
     print("created", output_filename_bulk)   

pdfs = ['TOC.pdf', 'bulk.pdf']

merger = PdfFileMerger()

for pdf in pdfs:
    merger.append(pdf)

merger.write("result.pdf")
merger.close()
以下是我重新排列PDF文档(TOC=目录)的代码:

从PyPDF2导入PdfileReader、PdfileWriter、PdfileMerger
PDF_report=PdfFileReader('file.PDF'))
pdf_bulk_writer=PdfFileWriter()
输出\u filename\u bulk=“bulk.pdf”
pdf_TOC_writer=PdfFileWriter()
输出\u filename\u TOC=“TOC.pdf”
对于范围内的页面(PDF_report.getNumPages()):
当前页面=PDF\u报告.getPage(第页)
如果page==PDF_report.getNumPages()-1:
pdf_TOC_writer.addPage(当前页面)

如果佩奇看了一个类似的问题,那就看一个类似的问题