แก้ไขไฟล์ PDF ผ่าน Python

ข้อมูลการแก้ไขที่ละเอียดอ่อนของเอกสาร PDFใช้ Aspose.PDF สำหรับ Python for .NET เพื่อแก้ไขเอกสาร PDF แบบโปรแกรม

วิธีการแก้ไขไฟล์ PDF โดยใช้ไลบรารี Python

หากต้องการแก้ไขไฟล์ PDF ให้ใช้ Aspose.PDF for Python via .NET ซึ่งเป็น API ที่มีประสิทธิภาพและใช้งานง่าย เปิด PyPI ค้นหา aspose-pdf และติดตั้ง หรือเรียกใช้คำสั่งต่อไปนี้:

แก้ไขเอกสาร PDF ผ่าน Python


คุณจำเป็นต้อง Aspose.PDF for .NET ที่จะลองรหัสในสภาพแวดล้อมของคุณ

1.โหลดไฟล์ PDF ที่มีอินสแตนซ์ของเอกสาร 1.สร้างวัตถุ TextFragmenTabSorber ด้วยคำค้นหาเป็นอาร์กิวเมนต์ 1.ตั้งค่าตัวเลือกการค้นหา 1.ห่วงผ่านแต่ละส่วนเก็บรวบรวมเพื่อทำซ้ำ 1.บันทึกไฟล์ PDF

แก้ไขไฟล์ PDF - Python

import aspose.pdf as apdf

from os import path
path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)

searchTerm = "Secret word"
textFragmentAbsorber = apdf.text.TextFragmentAbsorber(searchTerm)
textSearchOptions = apdf.text.TextSearchOptions(True)
textFragmentAbsorber.text_search_options = textSearchOptions

document.pages.accept(textFragmentAbsorber)
textFragmentCollection = textFragmentAbsorber.text_fragments
for textFragment in textFragmentCollection:
    page = textFragment.page
    annotationRectangle = textFragment.rectangle
    annot = apdf.annotations.RedactionAnnotation(page, annotationRectangle)
    annot.fill_color = apdf.Color.black
    document.pages[page.number].annotations.add(annot, True)
    annot.redact()

    document.save(path_outfile)