使大的 DOC 文件更小而不损失质量。删除不必要和未使用的数据。在代码中缩小 DOC 文件的大小。 Python 库为开发人员提供了一个集成的 API 来优化 DOC 内容。
Python 库是一个独立的解决方案,不需要安装任何第三方软件。
使用 'Aspose.Words for Python via .NET' 可以轻松实现无损压缩和深度内容优化。以下示例演示了如何优化 Python 中的 Python DOC 文件的内容:
pip install aspose-words 复制 import aspose.words as aw doc = aw.Document("Input.doc") 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.doc") import aspose.words as aw doc = aw.Document("Input.doc") 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.doc", save_options) import aspose.words as aw doc = aw.Document("Input.doc") 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.doc") import aspose.words as aw doc = aw.Document("Input.doc") 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.doc", save_options) import aspose.words as aw doc = aw.Document() builder = aw.DocumentBuilder(doc) shape = builder.InsertImage("Input.doc") save_options = aw.saving.ImageSaveOptions(aw.SaveFormat.DOC) shape.get_shape_renderer().save("Output.doc", 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.doc", '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.doc", 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 的附加要求。