使大的 HTML 文件更小而不損失質量。刪除不必要和未使用的數據。在代碼中縮小 HTML 文件的大小。 Python 庫為開發人員提供了一個集成的 API 來優化 HTML 內容。
Python 庫是一個獨立的解決方案,不需要安裝任何第三方軟件。
使用 'Aspose.Words for Python via .NET' 可以輕鬆實現無損壓縮和深度內容優化。以下示例演示瞭如何優化 Python 中的 Python HTML 文件的內容:
pip install aspose-words
複製
import aspose.words as aw
doc = aw.Document("Input.html")
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.html")
import aspose.words as aw
doc = aw.Document("Input.html")
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.html", save_options)
import aspose.words as aw
doc = aw.Document("Input.html")
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.html")
import aspose.words as aw
doc = aw.Document("Input.html")
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.html", save_options)
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
shape = builder.InsertImage("Input.html")
save_options = aw.saving.ImageSaveOptions(aw.SaveFormat.HTML)
shape.get_shape_renderer().save("Output.html", 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.html", '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.html", 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 的附加要求。