Hướng dẫn python add text to pdf - python thêm văn bản vào pdf

Tôi cần thêm một số văn bản bổ sung vào PDF hiện có bằng Python, cách tốt nhất để thực hiện điều này và tôi sẽ cần cài đặt các mô -đun bổ sung nào.

Lưu ý: Lý tưởng nhất là tôi muốn có thể chạy điều này trên cả Windows và Linux, nhưng tại một Linux đẩy sẽ chỉ làm được.

Chỉnh sửa: PYPDF và Báo cáo có vẻ tốt nhưng không ai cho phép tôi chỉnh sửa PDF hiện có, có tùy chọn nào khác không?

Hỏi ngày 24 tháng 7 năm 2009 lúc 20:58Jul 24, 2009 at 20:58

Hướng dẫn python add text to pdf - python thêm văn bản vào pdf

Ví dụ cho [Python 2.7]:

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()

Ví dụ cho 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()

Đã trả lời ngày 9 tháng 7 năm 2013 lúc 0:16Jul 9, 2013 at 0:16

David Dehghandavid DehghanDavid Dehghan

20.1k8 Huy hiệu vàng101 Huy hiệu bạc91 Huy hiệu Đồng8 gold badges101 silver badges91 bronze badges

10

Tôi biết đây là một bài viết cũ hơn, nhưng tôi đã dành một thời gian dài để cố gắng tìm một giải pháp. Tôi đã bắt gặp một cái tốt chỉ bằng cách sử dụng báo cáo và pypdf vì vậy tôi nghĩ rằng tôi sẽ chia sẻ:

  1. Đọc pdf của bạn bằng PdfFileReader(), chúng tôi sẽ gọi đầu vào này
  2. Tạo PDF mới chứa văn bản của bạn để thêm bằng cách sử dụng Báo cáo, lưu này dưới dạng đối tượng chuỗi
  3. Đọc đối tượng chuỗi bằng cách sử dụng PdfFileReader(), chúng tôi sẽ gọi văn bản này
  4. Tạo một đối tượng PDF mới bằng cách sử dụng PdfFileWriter(), chúng tôi sẽ gọi đầu ra này
  5. Lặp lại thông qua đầu vào và áp dụng .mergePage(*text*.getPage(0)) cho mỗi trang bạn muốn văn bản được thêm vào, sau đó sử dụng
    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()
    
    0 để thêm các trang sửa đổi vào một tài liệu mới

Điều này hoạt động tốt cho việc bổ sung văn bản đơn giản. Xem mẫu của PYPDF để đánh dấu một tài liệu.

Dưới đây là một số mã để trả lời câu hỏi dưới đây:

packet = StringIO.StringIO()
can = canvas.Canvas(packet, pagesize=letter)

can.save()
packet.seek(0)
input = PdfFileReader(packet)

Từ đây, bạn có thể hợp nhất các trang của tệp đầu vào với một tài liệu khác.

Đã trả lời ngày 1 tháng 2 năm 2010 lúc 23:28Feb 1, 2010 at 23:28

Dwelchdwelchdwelch

2.9563 huy hiệu vàng20 Huy hiệu bạc13 Huy hiệu đồng3 gold badges20 silver badges13 bronze badges

2

PDFRW sẽ cho phép bạn đọc trong các trang từ PDF hiện có và vẽ chúng vào một khung vẽ báo cáo (tương tự như vẽ một hình ảnh). Có những ví dụ cho điều này trong các ví dụ PDFRW/thư mục RL1 trên GitHub. Tuyên bố miễn trừ trách nhiệm: Tôi là tác giả PDFRW.

Đã trả lời ngày 11 tháng 7 năm 2015 lúc 4:47Jul 11, 2015 at 4:47

Hướng dẫn python add text to pdf - python thêm văn bản vào pdf

Patrick Maupinpatrick MaupinPatrick Maupin

7.8952 Huy hiệu vàng22 Huy hiệu bạc42 Huy hiệu đồng2 gold badges22 silver badges42 bronze badges

1

CPDF sẽ thực hiện công việc từ dòng lệnh. Mặc dù vậy, nó không phải là Python (afaik):

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

Đã trả lời ngày 5 tháng 3 năm 2014 lúc 11:51Mar 5, 2014 at 11:51

user2243670user2243670user2243670

3452 Huy hiệu bạc8 Huy hiệu Đồng2 silver badges8 bronze badges

1

Tận dụng câu trả lời của David Dehghan ở trên, các tác phẩm sau đây trong 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()

Đã trả lời ngày 22 tháng 4 năm 2017 lúc 21:52Apr 22, 2017 at 21:52

Ross Smith iirross Smith IIRoss Smith II

Huy hiệu vàng 11,6K11 gold badge37 silver badges42 bronze badges

1

Không sử dụng MergePage, nó có thể không hoạt động đối với một số tệp PDF bạn nên sử dụng MergerotatedTranslatedPage

from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen.canvas import Canvas

page_to_merge = 0 #Refers to the First page of PDF 
xcoor = 250 #To be changed according to your pdf
ycoor = 650 #To be changed according to your pdf

input_pdf = PdfFileReader(open("Source.pdf", "rb"))
page_count = input_pdf.getNumPages()
inputpdf_page_to_be_merged = input_pdf.getPage(page_to_merge)

packet = io.BytesIO()
c = Canvas(packet,pagesize=(inputpdf_page_to_be_merged.mediaBox.getWidth(),inputpdf_page_to_be_merged.mediaBox.getHeight()))
c.drawString(xcoor,ycoor,"Hello World")
c.save()
packet.seek(0)

overlay_pdf = PdfFileReader(packet)
overlay = overlay_pdf.getPage(0)

output = PdfFileWriter()

for PAGE in range(page_count):
    if PAGE == page_to_merge:
        inputpdf_page_to_be_merged.mergeRotatedTranslatedPage(overlay, 
                inputpdf_page_to_be_merged.get('/Rotate') or 0, 
                overlay.mediaBox.getWidth()/2, overlay.mediaBox.getWidth()/2)
        output.addPage(inputpdf_page_to_be_merged)
    
    else:
        Page_in_pdf = input_pdf.getPage(PAGE)
        output.addPage(Page_in_pdf)

outputStream = open("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()

Đã trả lời ngày 23 tháng 2 lúc 10:37Feb 23 at 10:37

Bạn có thể gặp may mắn hơn khi phá vỡ vấn đề thành chuyển đổi PDF thành một định dạng có thể chỉnh sửa, viết các thay đổi của bạn, sau đó chuyển đổi nó trở lại thành PDF. Tôi không biết một thư viện cho phép bạn chỉnh sửa trực tiếp PDF nhưng có rất nhiều trình chuyển đổi giữa Doc và PDF chẳng hạn.

Đã trả lời ngày 24 tháng 7 năm 2009 lúc 21:03Jul 24, 2009 at 21:03

Aehlkeaehlkeaehlke

14.7K5 Huy hiệu vàng34 Huy hiệu bạc44 Huy hiệu đồng5 gold badges34 silver badges44 bronze badges

4

Python có thể hoạt động với pdfs không?

Bạn có thể làm việc với PDF có sẵn trong Python bằng cách sử dụng gói PYPDF2.PYPDF2 là gói python thuần túy mà bạn có thể sử dụng cho nhiều loại hoạt động PDF khác nhau.. PyPDF2 is a pure-Python package that you can use for many different types of PDF operations.

Python có thể tạo báo cáo PDF không?

Tạo báo cáo PDF với đồ thị Plotly và Python.Vì đồ thị Plotly có thể được nhúng trong HTML hoặc được xuất dưới dạng hình ảnh tĩnh, bạn có thể nhúng các biểu đồ cốt truyện trong các báo cáo phù hợp để in và cho web.Sổ ghi chép này là một đoạn mồi trong việc tạo các báo cáo PDF với Python từ HTML với đồ thị Plotly.you can embed Plotly graphs in reports suited for print and for the web. This notebook is a primer on creating PDF reports with Python from HTML with Plotly graphs.