Kode kanggo ngompres PDF ing Python

Pustaka Python berkinerja tinggi kanggo ngoptimalake isi lan ukuran PDF njaga kualitas asli lan data dasar

Iki minangka solusi profesional kanggo ngompres PDF nganggo Python. Gunakake API optimasi PDF kita kanggo ngembangake piranti lunak bebas platform tingkat dhuwur ing Python. Coba online gratis!

Ndeleng potongan kode

Kompres PDF ing Python

Pustaka Python iki nyedhiyakake pangembang kanthi kemampuan kanggo ngompres PDF dokumen kanthi cepet lan efisien kanthi program. Gampang entuk output PDF sing dioptimalake siap kanggo dicithak, diarsipake utawa dienggo bareng liwat jaringan. Iki minangka solusi pangolahan PDF dokumen mandiri kanggo Python sing ora mbutuhake piranti lunak pihak katelu kanggo diinstal.

Ngurangi ukuran PDF nggunakake Python

Produk kita nyedhiyakake pangembang Python kanthi API terpadu kanggo nyuda ukuran PDF file. Kompresi PDF ditindakake kanthi rong pass. Sajrone pass pisanan, kabeh kaca PDF diowahi dadi format gambar. Produk kita nggunakake algoritma kompresi gambar sing adaptif lan efisien banget sing ngidini pangembang entuk output PDF sing kompak nalika njaga kualitas dokumen sing optimal. Ing pass sabanjure, urutan gambar dikompres lan digabungake kanggo mbentuk isi PDF file anyar sing dioptimalake.

Kompresi PDF digawe gampang karo 'Aspose.Words for Python via .NET'. Conto ing ngisor iki nuduhake carane nyuda ukuran PDF file ing Python:

Conto kode ing Python kanggo ngompres PDF file
Unggah file sing pengin dioptimalake / kompres
Kode roto
Pilih format target saka dhaptar
50%
pip install aspose-words
Nyalin
import aspose.words as aw

renderer = aw.pdf2word.fixedformats.PdfFixedRenderer()
pdf_read_options = aw.pdf2word.fixedformats.PdfFixedOptions()
pdf_read_options.image_format = aw.pdf2word.fixedformats.FixedImageFormat.JPEG
pdf_read_options.jpeg_quality = 50

with open ("Input.pdf", 'rb') as pdf_stream:
    pages_stream = renderer.save_pdf_as_images(pdf_stream, pdf_read_options);

builder = aw.DocumentBuilder()
for i in range(0, len(pages_stream)):
    # Setel ukuran kaca maksimal kanggo ngindhari skala gambar kaca saiki.
    max_page_dimension = 1584
    page_setup = builder.page_setup
    set_page_size(page_setup, max_page_dimension, max_page_dimension)

    page_image = builder.insert_image(pages_stream[i])

    set_page_size(page_setup, page_image.width, page_image.height)
    page_setup.top_margin = 0
    page_setup.left_margin = 0
    page_setup.bottom_margin = 0
    page_setup.right_margin = 0

    if i != len(pages_stream) - 1:
        builder.insert_break(aw.BreakType.SECTION_BREAK_NEW_PAGE)

save_options = aw.saving.PdfSaveOptions()
save_options.cache_background_graphics = true

builder.document.save("Output.pdf", save_options)

def set_page_size(page_setup, width, height):

    page_setup.page_width = width;
    page_setup.page_height = height;
import aspose.words as aw doc = aw.Document("Input.pdf") doc.cleanup() shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)] for shape in shapes: if shape.isImage: # Terserah pangembang kanggo milih perpustakaan kanggo kompresi gambar. image = Image.open(shape.image_data.to_stream()) # ... # Kompres gambar lan setel maneh menyang wangun. shape.image_data.set_image("yourCompressedImage") save_options = aw.saving.PdfSaveOptions save_options.cache_background_graphics = true doc.save("Output.pdf", save_options) import aspose.words as aw doc = aw.Document("Input.pdf") doc.cleanup() shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)] for shape in shapes: if shape.isImage: # Terserah pangembang kanggo milih perpustakaan kanggo kompresi gambar. image = Image.open(shape.image_data.to_stream()) # ... # Kompres gambar lan setel maneh menyang wangun. shape.image_data.set_image("yourCompressedImage") doc.save("Output.pdf") import aspose.words as aw doc = aw.Document("Input.pdf") doc.cleanup() shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)] for shape in shapes: if shape.isImage: # Terserah pangembang kanggo milih perpustakaan kanggo kompresi gambar. image = Image.open(shape.image_data.to_stream()) # ... # Kompres gambar lan setel maneh menyang wangun. shape.image_data.set_image("yourCompressedImage") save_options = aw.saving.OoxmlSaveOptions save_options.compression_level = aw.saving.CompressionLevel.MAXIMUM doc.save("Output.pdf", save_options) import aspose.words as aw doc = aw.Document() builder = aw.DocumentBuilder(doc) shape = builder.InsertImage("Input.pdf") save_options = aw.saving.ImageSaveOptions(aw.SaveFormat.PDF) shape.get_shape_renderer().save("Output.pdf", save_options) import aspose.words as aw renderer = aw.pdf2word.fixedformats.PdfFixedRenderer() pdf_read_options = aw.pdf2word.fixedformats.PdfFixedOptions() pdf_read_options.image_format = aw.pdf2word.fixedformats.FixedImageFormat.JPEG pdf_read_options.jpeg_quality = 50 with open ("Input.pdf", 'rb') as pdf_stream: pages_stream = renderer.save_pdf_as_images(pdf_stream, pdf_read_options); builder = aw.DocumentBuilder() for i in range(0, len(pages_stream)): # Setel ukuran kaca maksimal kanggo ngindhari skala gambar kaca saiki. max_page_dimension = 1584 page_setup = builder.page_setup set_page_size(page_setup, max_page_dimension, max_page_dimension) page_image = builder.insert_image(pages_stream[i]) set_page_size(page_setup, page_image.width, page_image.height) page_setup.top_margin = 0 page_setup.left_margin = 0 page_setup.bottom_margin = 0 page_setup.right_margin = 0 if i != len(pages_stream) - 1: builder.insert_break(aw.BreakType.SECTION_BREAK_NEW_PAGE) save_options = aw.saving.PdfSaveOptions() save_options.cache_background_graphics = true builder.document.save("Output.pdf", save_options) def set_page_size(page_setup, width, height): page_setup.page_width = width; page_setup.page_height = height;
Kode roto

Cara ngompres PDF Ing Python

  1. Pasang Aspose.Words for Python via .NET.
  2. Tambah referensi perpustakaan (ngimpor perpustakaan) menyang proyek Python sampeyan.
  3. Bukak PDF berkas sumber ing Python.
  4. Kompres PDF tanpa kelangan kualitas.
  5. Telpon metode document_cleanup(). Kode asil bisa beda-beda gumantung saka format file output.
  6. Entuk asil sing dikompres minangka PDF file.

Pustaka Python kanggo ngoptimalake lan ngompres PDF file

Kita dadi tuan rumah paket Python ing repositori PyPi. Mangga tindakake langkah-langkah dening-instruksi carane nginstal "Aspose.Words for Python via .NET" kanggo lingkungan pangembang.

Requirements Sistem

Paket iki kompatibel karo Python ≥3.5 lan <3.12. Yen sampeyan ngembangake piranti lunak kanggo Linux, deleng syarat tambahan kanggo gcc lan libpython ing Dokumentasi Produk.

Optimasi format file liyane sing didhukung

Sampeyan bisa ngoptimalake file ing akeh format file liyane:

5%

Langganan kanggo Aspose Update Product

Entuk buletin saben wulan lan tawaran langsung dikirim menyang kothak layang.

© Aspose Pty Ltd 2001-2024. Kabeh hak dilindhungi undhang-undhang.