Python を使用して既存の PDF にテキストを追加する方法
Python を使用して既存の PDF にテキストを追加
背景:
多くの場合、既存の PDF ドキュメントに追加のテキストを追加する必要が生じることがあります。幸いなことに、Python には、このタスクを簡素化するいくつかのモジュールが用意されています。ただし、Windows と Linux システムの両方と互換性のあるモジュールを特定することが重要です。
推奨モジュール:
さまざまなオプションを検討した結果、2 つの適切なモジュールは PyPDF2 と PyPDF2 です。 PyPDF。これらのモジュールは、高レベルの機能とクロスプラットフォームのサポートを提供します。
コード例:
以下は、Python 2.7 と Python 3 の両方のコード例です。 x:
Python 2.7:<code class="python">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()</code>
ログイン後にコピー
<code class="python">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()</code>
Python 3.x:<code class="python">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.pages[0]
page.merge_page(new_pdf.pages[0])
output.addPage(page)
# Finally, write "output" to a real file
output_stream = open("destination.pdf", "wb")
output.write(output_stream)
output_stream.close()</code>
ログイン後にコピー
<code class="python">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.pages[0] page.merge_page(new_pdf.pages[0]) output.addPage(page) # Finally, write "output" to a real file output_stream = open("destination.pdf", "wb") output.write(output_stream) output_stream.close()</code>
これらのコード例は、既存の PDF ファイルの最初のページにテキスト「Hello world」を追加し、結果を新しい PDF ファイルに保存します。テキストと位置はそれに応じてカスタマイズできます。
以上がPython を使用して既存の PDF にテキストを追加する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











fiddlereveryversings for the-middleの測定値を使用するときに検出されないようにする方法

10時間以内にコンピューター初心者プログラミングの基本を教える方法は?コンピューター初心者にプログラミングの知識を教えるのに10時間しかない場合、何を教えることを選びますか...

Pythonasyncioについて...

Investing.comの反クラウリング戦略を理解する多くの人々は、Investing.com(https://cn.investing.com/news/latest-news)からのニュースデータをクロールしようとします。

Python 3.6のピクルスファイルの読み込みエラー:modulenotfounderror:nomodulenamed ...

SCAPYクローラーを使用するときにパイプラインファイルを作成できない理由についての議論は、SCAPYクローラーを学習して永続的なデータストレージに使用するときに、パイプラインファイルに遭遇する可能性があります...
