See on professionaalne lahendus Word faili tihendamiseks Python abil. Kasutage Pythonis kõrgetasemelise platvormist sõltumatu tarkvara arendamiseks meie Word optimeerimise API t. Proovige seda veebis tasuta!
Muutke suured Word failid väiksemaks ilma kvaliteeti kaotamata. Kustutage mittevajalikud ja kasutamata andmed. Vähendage koodis Word faili suurust. Python teek pakub arendajatele integreeritud API d Word sisu optimeerimiseks.
Python teek on iseseisev lahendus ja see ei nõua kolmanda osapoole tarkvara installimist.
'Aspose.Words for Python via .NET' kadudeta pakkimise ja sisu sügava optimeerimise lihtsaks. Järgmine näide demonstreerib, kuidas optimeerida Word faili Python:
pip install aspose-words
Kopeeri
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:
# Kujutise tihendamiseks teegi valib arendaja.
image = Image.open(shape.image_data.to_stream())
# ...
# Tihendage pilt ja seadke see kuju tagasi.
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:
# Kujutise tihendamiseks teegi valib arendaja.
image = Image.open(shape.image_data.to_stream())
# ...
# Tihendage pilt ja seadke see kuju tagasi.
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:
# Kujutise tihendamiseks teegi valib arendaja.
image = Image.open(shape.image_data.to_stream())
# ...
# Tihendage pilt ja seadke see kuju tagasi.
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:
# Kujutise tihendamiseks teegi valib arendaja.
image = Image.open(shape.image_data.to_stream())
# ...
# Tihendage pilt ja seadke see kuju tagasi.
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")
save_options = aw.saving.ImageSaveOptions(aw.SaveFormat.DOCX)
shape.get_shape_renderer().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)):
# Määrake lehe maksimaalne suurus, et vältida praeguse lehe kujutise skaleerimist.
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;
Hoidame oma Python pakette PyPi hoidlates. Järgige samm-sammult juhiseid selle kohta, kuidas installida "Aspose.Words for Python via .NET" kaudu oma arendajakeskkonda.
See pakett ühildub Python ≥3.5 ja <3.12. Kui arendate tarkvara Linuxi jaoks, vaadake tootedokumentatsioonis gcc ja libpython lisanõudeid.
Saate optimeerida faile paljudes teistes failivormingutes: