programing tip

Python을 사용하여 기존 PDF에 텍스트 추가

itbloger 2020. 9. 1. 07:17
반응형

Python을 사용하여 기존 PDF에 텍스트 추가


Python을 사용하여 기존 PDF에 추가 텍스트를 추가해야합니다.이 문제를 해결하는 가장 좋은 방법은 무엇이며 어떤 추가 모듈을 설치해야할까요?

참고 : 이상적으로는 Windows와 Linux 모두에서 실행할 수 있기를 원하지만 푸시에서는 Linux 만 실행할 수 있습니다.

편집 : pyPDFReportLab 은 좋아 보이지만 둘 다 기존 PDF를 편집 할 수 없습니다. 다른 옵션이 있습니까?


나는 이것이 오래된 게시물이라는 것을 알고 있지만 해결책을 찾기 위해 오랜 시간을 보냈습니다. ReportLab과 PyPDF 만 사용하여 괜찮은 것을 발견 했으므로 공유 할 것이라고 생각했습니다.

  1. 을 사용하여 PDF를 읽으십시오. PdfFileReader()입력을
  2. ReportLab을 사용하여 추가 할 텍스트가 포함 된 새 pdf를 만들고이를 문자열 개체로 저장합니다.
  3. 사용하여 문자열 객체를 읽어 PdfFileReader(), 우리는이 전화 할게 텍스트를
  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)

여기에서 입력 파일의 페이지를 다른 문서와 병합 할 수 있습니다.


[Python 2.7]의 예 :

from pyPdf import PdfFileWriter, PdfFileReader
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(10, 100, "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(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()
# create a new PDF with Reportlab
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)
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()

pdfrw will let you read in pages from an existing PDF and draw them to a reportlab canvas (similar to drawing an image). There are examples for this in the pdfrw examples/rl1 subdirectory on github. Disclaimer: I am the pdfrw author.


Leveraging David Dehghan's answer above, the following works in 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()

cpdf will do the job from the command-line. It isn't python, though (afaik):

cpdf -add-text "Line of text" input.pdf -o output .pdf

You may have better luck breaking the problem down into converting PDF into an editable format, writing your changes, then converting it back into PDF. I don't know of a library that lets you directly edit PDF but there are plenty of converters between DOC and PDF for example.


If you're on Windows, this might work:

PDF Creator Pilot

There's also a whitepaper of a PDF creation and editing framework in Python. It's a little dated, but maybe can give you some useful info:

Using Python as PDF Editing and Processing Framework


Have you tried pyPdf ?

Sorry, it doesn’t have the ability to modify a page’s content.

참고URL : https://stackoverflow.com/questions/1180115/add-text-to-existing-pdf-using-python

반응형