Sử dụng API tối ưu hóa tài liệu của chúng tôi để phát triển phần mềm cấp cao, độc lập với nền tảng bằng Python. Đây là một giải pháp mạnh mẽ để giảm kích thước tài liệu và hình ảnh bằng Python.
Làm cho các tệp lớn nhỏ hơn mà không làm giảm chất lượng. Xóa dữ liệu không cần thiết và không sử dụng. Thu nhỏ kích thước của Word tệp và HTML trong Python. Thư viện Python của chúng tôi cung cấp cho các nhà phát triển một API tích hợp để tối ưu hóa tài liệu ở nhiều định dạng khác nhau.
Thư viện Python này là một giải pháp độc lập và không yêu cầu cài đặt bất kỳ phần mềm bên thứ ba nào.
Hiệu ứng tối đa có thể đạt được bằng cách tối ưu hóa các hình ảnh có độ phân giải cao, cũng như các tài liệu có chứa các hình ảnh đó. Thư viện Python của chúng tôi sẽ giúp bạn nén các DOCX tệp, DOC, RTF, ODT, EPUB, HTML, JPG, PNG.
Việc tối ưu hóa tài liệu mà không làm giảm chất lượng trở nên dễ dàng với 'Aspose.Words for Python via .NET'. Ví dụ sau cho thấy cách thu nhỏ kích thước của Word tài liệu trong Python:
import aspose.words as aw
doc = aw.Document("Input.docx")
doc.cleanup()
shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)]
for shape in shapes:
if shape.isImage:
# Tùy nhà phát triển chọn thư viện để nén ảnh.
image = Image.open(shape.image_data.to_stream())
# ...
# Nén hình ảnh và đặt nó trở lại hình dạng.
shape.image_data.set_image("yourCompressedImage")
save_options = aw.saving.OoxmlSaveOptions
save_options.compression_level = aw.saving.CompressionLevel.MAXIMUM
doc.save("Output.docx", save_options)
import aspose.words as aw
doc = aw.Document("Input.docx")
doc.cleanup()
shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)]
for shape in shapes:
if shape.isImage:
# Tùy nhà phát triển chọn thư viện để nén ảnh.
image = Image.open(shape.image_data.to_stream())
# ...
# Nén hình ảnh và đặt nó trở lại hình dạng.
shape.image_data.set_image("yourCompressedImage")
save_options = aw.saving.PdfSaveOptions
save_options.cache_background_graphics = true
doc.save("Output.docx", save_options)
import aspose.words as aw
doc = aw.Document("Input.docx")
doc.cleanup()
shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)]
for shape in shapes:
if shape.isImage:
# Tùy nhà phát triển chọn thư viện để nén ảnh.
image = Image.open(shape.image_data.to_stream())
# ...
# Nén hình ảnh và đặt nó trở lại hình dạng.
shape.image_data.set_image("yourCompressedImage")
doc.save("Output.docx")
import aspose.words as aw
doc = aw.Document("Input.docx")
doc.cleanup()
shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)]
for shape in shapes:
if shape.isImage:
# Tùy nhà phát triển chọn thư viện để nén ảnh.
image = Image.open(shape.image_data.to_stream())
# ...
# Nén hình ảnh và đặt nó trở lại hình dạng.
shape.image_data.set_image("yourCompressedImage")
save_options = aw.saving.OoxmlSaveOptions
save_options.compression_level = aw.saving.CompressionLevel.MAXIMUM
doc.save("Output.docx", save_options)
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
shape = builder.InsertImage("Input.docx")
# Cập nhật cài đặt trang để cắt tài liệu cho vừa với kích thước của hình ảnh.
pageSetup = builder.page_setup
pageSetup.page_width = shape.width
pageSetup.page_height = shape.height
pageSetup.top_margin = 0
pageSetup.left_margin = 0
pageSetup.bottom_margin = 0
pageSetup.right_margin = 0
save_options = aw.saving.ImageSaveOptions(aw.SaveFormat.DOCX)
doc.save("Output.docx", 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.docx", '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)):
# Đặt kích thước trang tối đa để tránh chia tỷ lệ hình ảnh trang hiện tại.
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.docx", save_options)
def set_page_size(page_setup, width, height):
page_setup.page_width = width;
page_setup.page_height = height;
Chúng tôi lưu trữ các gói Python trong kho lưu trữ PyPi. Vui lòng làm theo hướng dẫn từng bước về cách cài đặt "Aspose.Words for Python via .NET" vào môi trường nhà phát triển của bạn.
Gói này tương thích với Python ≥3.5 và <3.12. Nếu bạn phát triển phần mềm cho Linux, vui lòng xem các yêu cầu bổ sung đối với gcc và libpython trong Tài liệu sản phẩm.