แก้ไขไฟล์ PDF โดยใช้ Python

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

วิธีแก้ไขไฟล์ PDF โดยใช้ห้องสมุด Python

ในการแก้ไขไฟล์ PDF ให้ใช้ Aspose.PDF for Python ผ่าน.NET API ที่ทรงพลังและใช้งานง่ายเปิด PyPi ค้นหา aspose-pdf แล้วติดตั้งหรือเรียกใช้คำสั่ง:

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


คุณต้อง Aspose.PDF for Python ผ่าน.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)