Đây là một giải pháp chuyên nghiệp để nén EPUB bằng Python. Sử dụng EPUB API tối ưu hóa 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. Hãy thử nó trực tuyến miễn phí!
Chúng tôi hiện không chấp nhận tệp DOCX làm đầu vào. Nhưng chúng tôi có thể sản xuất các tệp DOCX như một sự bổ sung. Chúng tôi mong muốn được ủng hộ DOCX đầy đủ trong thời gian sắp tới.
Thư viện Python 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.
Thu nhỏ tệp DOCX mà không làm giảm chất lượng. Thư viện Python cung cấp cho các nhà phát triển một API tích hợp để tối ưu hóa nội dung DOCX. Tối ưu hóa DOCX xảy ra bằng cách nén hình ảnh bên trong tệp DOCX, cũng như bằng cách xóa dữ liệu không sử dụng.
Việc nén không mất dữ liệu điện tử và tối ưu hóa nội dung trở nên dễ dàng với 'Aspose.Words for Python via .NET'. Ví dụ sau minh họa cách tối ưu hóa nội dung của tệp DOCX trong Python:
pip install aspose-words
Sao chép
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.epub")
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.epub", 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.epub")
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.epub", save_options)
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
shape = builder.InsertImage("Input.docx")
save_options = aw.saving.ImageSaveOptions(aw.SaveFormat.DOCX)
shape.get_shape_renderer().save("Output.epub", 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.epub", 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.