我們目前不接受 DOCX 文件作為輸入。但是我們可以生成 DOCX 文件作為輸出。我們期待在不久的將來全面支持 DOCX。
Python 庫是一個獨立的解決方案,不需要安裝任何第三方軟件。
在不損失質量的情況下使 DOCX 文件更小。 Python 庫為開發人員提供了一個集成的 API 來優化 DOCX 內容。 DOCX 優化通過壓縮 DOCX 文件中的圖像以及刪除未使用的數據來實現。
使用 'Aspose.Words for Python via .NET' 輕鬆實現電子書無損壓縮和內容優化。以下示例演示瞭如何在 Python 中優化 DOCX 文件的內容:
pip install aspose-words
複製
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:
# 由開發者選擇用於圖像壓縮的庫。
image = Image.open(shape.image_data.to_stream())
# ...
# 壓縮圖像並將其設置回形狀。
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:
# 由開發者選擇用於圖像壓縮的庫。
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.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:
# 由開發者選擇用於圖像壓縮的庫。
image = Image.open(shape.image_data.to_stream())
# ...
# 壓縮圖像並將其設置回形狀。
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:
# 由開發者選擇用於圖像壓縮的庫。
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.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)):
# 設置最大頁面大小,避免當前頁面圖像縮放。
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;
我們在PyPi存儲庫中託管我們的 Python 請按照有關如何 "Aspose.Words for Python via .NET" 安裝到您的開發人員環境的分步說明進行操作。
這個包與 Python ≥3.5 和 <3.12 兼容。如果您為 Linux 開發軟件,請查看產品文檔中對 gcc 和 libpython 的附加要求。