這個 Python 庫為開發人員提供了以編程方式快速有效地壓縮 PDF 文檔的能力。輕鬆獲得優化的 PDF 輸出,以便通過網絡進行打印、存檔或共享。它是一個用於 Python 的獨立 PDF 文檔處理解決方案,不需要安裝任何第三方軟件。
我們的產品為 Python 開發人員提供了一個集成的 API,以減小 PDF 文件的大小。 PDF 壓縮分兩次執行。在第一遍中,PDF 的所有頁面都轉換為圖像格式。我們的產品使用自適應和高效的圖像壓縮算法,使開發人員能夠獲得緊湊的 PDF 輸出,同時保持文檔的最佳質量。在下一輪中,圖像序列被壓縮並組合成一個新的、優化的 PDF 文件的內容。
'Aspose.Words for Python via .NET' 使 PDF 壓縮變得容易。以下示例演示如何在 Python 中減小 PDF 文件的大小:
pip install aspose-words
複製
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)):
# 設置最大頁面大小,避免當前頁面圖像縮放。
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:
# 由開發者選擇用於圖像壓縮的庫。
image = Image.open(shape.image_data.to_stream())
# ...
# 壓縮圖像並將其設置回形狀。
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:
# 由開發者選擇用於圖像壓縮的庫。
image = Image.open(shape.image_data.to_stream())
# ...
# 壓縮圖像並將其設置回形狀。
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:
# 由開發者選擇用於圖像壓縮的庫。
image = Image.open(shape.image_data.to_stream())
# ...
# 壓縮圖像並將其設置回形狀。
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)):
# 設置最大頁面大小,避免當前頁面圖像縮放。
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;
我們在PyPi存儲庫中託管我們的 Python 請按照有關如何 "Aspose.Words for Python via .NET" 安裝到您的開發人員環境的分步說明進行操作。
這個包與 Python ≥3.5 和 <3.12 兼容。如果您為 Linux 開發軟件,請查看產品文檔中對 gcc 和 libpython 的附加要求。