Python:使用PyPDF2和io为PDF中的页面编号

时间:2018-12-20 08:04:45

标签: python pypdf2

因此,我试图将页面编号追溯添加到PDF文件中。我不明白这是怎么回事。 我从herehere复制了代码。 我遇到一个我似乎无法自行解决的问题,可能是因为即使阅读了PyPDF2文档,我也不了解正在发生的事情。

from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4


packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=A4)    
can.drawString(10, 100, "Page" + str(15)) #just a random test number
can.save()
packet.seek(0)

watermark = PdfFileReader(packet)
watermark_page = watermark.getPage(0)

pdf = PdfFileReader('in.pdf')
pdf_writer = PdfFileWriter()

for page in range(pdf.getNumPages()):

    pdf_page = pdf.getPage(page)
    pdf_page.mergePage(watermark_page)
    pdf_writer.addPage(pdf_page)

with open('out.pdf', 'wb') as fh:
    pdf_writer.write(fh)

这很好。但是,我想给每个页面一个不同的数字。所以我将for循环更改为:

from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4

packet = io.BytesIO()

pdf = PdfFileReader('in.pdf')
pdf_writer = PdfFileWriter()

for page in range(pdf.getNumPages()):

    can = canvas.Canvas(packet, pagesize=A4)


    can.drawString(10, 200, "Page " + str(page) )
    can.save()
    packet.seek(0)
    watermark = PdfFileReader(packet)
    watermark_page = watermark.getPage(0)



    pdf_page = pdf.getPage(page)
    pdf_page.mergePage(watermark_page)
    pdf_writer.addPage(pdf_page)

with open('out.pdf', 'wb') as fh:
    pdf_writer.write(fh)

这不起作用。

我得到:

Traceback (most recent call last):

  File "<ipython-input-44-c6a76740be9f>", line 1, in <module>
    runfile('//DIR/pdftest.py', wdir='//DIR')

  File "C:\Program Files (x86)\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Program Files (x86)\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "//DIR/pdftest.py", line 55, in <module>
    watermark = PdfFileReader(packet)

  File "C:\Program Files (x86)\Anaconda\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__
    self.read(stream)

  File "C:\Program Files (x86)\Anaconda\lib\site-packages\PyPDF2\pdf.py", line 1901, in read
    raise utils.PdfReadError("Could not find xref table at specified location")

PdfReadError: Could not find xref table at specified location

非常感谢您帮助我们理解和解决此问题。

谢谢!

0 个答案:

没有答案