代码之家  ›  专栏  ›  技术社区  ›  Frozenskys

使用Python向现有PDF添加文本

  •  78
  • Frozenskys  · 技术社区  · 17 年前

    我需要使用Python向现有PDF添加一些额外的文本,最好的方法是什么,需要安装哪些额外的模块。

    编辑: pyPDF ReportLab

    8 回复  |  直到 7 年前
        1
  •  155
  •   David Dehghan    4 年前


    from pyPdf import PdfFileWriter, PdfFileReader
    import StringIO
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter
    
    packet = StringIO.StringIO()
    can = canvas.Canvas(packet, pagesize=letter)
    can.drawString(10, 100, "Hello world")
    can.save()
    
    #move to the beginning of the StringIO buffer
    packet.seek(0)
    
    # create a new PDF with Reportlab
    new_pdf = PdfFileReader(packet)
    # read your existing PDF
    existing_pdf = PdfFileReader(file("original.pdf", "rb"))
    output = PdfFileWriter()
    # add the "watermark" (which is the new pdf) on the existing page
    page = existing_pdf.getPage(0)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)
    # finally, write "output" to a real file
    outputStream = file("destination.pdf", "wb")
    output.write(outputStream)
    outputStream.close()
    

    Python 3.x的示例:


    from PyPDF2 import PdfFileWriter, PdfFileReader
    import io
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter
    
    packet = io.BytesIO()
    can = canvas.Canvas(packet, pagesize=letter)
    can.drawString(10, 100, "Hello world")
    can.save()
    
    #move to the beginning of the StringIO buffer
    packet.seek(0)
    
    # create a new PDF with Reportlab
    new_pdf = PdfFileReader(packet)
    # read your existing PDF
    existing_pdf = PdfFileReader(open("original.pdf", "rb"))
    output = PdfFileWriter()
    # add the "watermark" (which is the new pdf) on the existing page
    page = existing_pdf.getPage(0)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)
    # finally, write "output" to a real file
    outputStream = open("destination.pdf", "wb")
    output.write(outputStream)
    outputStream.close()
    
        2
  •  98
  •   dwelch    7 年前

    我知道这是一篇老文章,但我花了很长时间试图找到一个解决方案。我发现了一个只使用ReportLab和PyPDF的不错的版本,所以我想与大家分享一下:

    1. 使用 PdfFileReader() ,我们称之为 输入
    2. 创建包含要使用ReportLab添加的文本的新pdf,并将其另存为字符串对象
    3. PdfileReader() ,我们称之为 文本
    4. 使用创建新的PDF对象 PdfFileWriter() ,我们称之为 输出
    5. 反复浏览 输入 申请 .mergePage(*text*.getPage(0)) 对于要添加文本的每个页面,请使用 output.addPage()

    这适用于简单的文本添加。请参阅PyPDF的示例以获取文档水印。

    以下是回答以下问题的一些代码:

    packet = StringIO.StringIO()
    can = canvas.Canvas(packet, pagesize=letter)
    <do something with canvas>
    can.save()
    packet.seek(0)
    input = PdfFileReader(packet)
    

    从这里可以将输入文件的页面与另一个文档合并。

        3
  •  13
  •   Patrick Maupin    10 年前

    pdfrw examples/rl1 github上的子目录。免责声明:我是pdfrw的作者。

        4
  •  8
  •   Community Mohan Dere    9 年前

    杠杆作用 David Dehghan answer 上面介绍了Python 2.7.13中的以下工作:

    from PyPDF2 import PdfFileWriter, PdfFileReader, PdfFileMerger
    
    import StringIO
    
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter
    
    packet = StringIO.StringIO()
    # create a new PDF with Reportlab
    can = canvas.Canvas(packet, pagesize=letter)
    can.drawString(290, 720, "Hello world")
    can.save()
    
    #move to the beginning of the StringIO buffer
    packet.seek(0)
    new_pdf = PdfFileReader(packet)
    # read your existing PDF
    existing_pdf = PdfFileReader("original.pdf")
    output = PdfFileWriter()
    # add the "watermark" (which is the new pdf) on the existing page
    page = existing_pdf.getPage(0)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)
    # finally, write "output" to a real file
    outputStream = open("destination.pdf", "wb")
    output.write(outputStream)
    outputStream.close()
    
        5
  •  7
  •   user2243670    12 年前

    cpdf

    cpdf -add-text "Line of text" input.pdf -o output .pdf
    
        6
  •  0
  •   aehlke    17 年前

    您可能会更幸运地将问题分解为将PDF转换为可编辑格式,编写更改,然后将其转换回PDF。我不知道有哪一个库可以让你直接编辑PDF,但是在文档和PDF之间有很多转换器。

        7
  •  0
  •   Community Mohan Dere    11 年前

    如果您在Windows上,这可能会起作用:

    PDF Creator Pilot

    还有一份关于Python中PDF创建和编辑框架的白皮书。虽然有点过时,但也许可以给你一些有用的信息:

    Using Python as PDF Editing and Processing Framework