Convert EPUB to DOCX in Python
Converting EPUB to DOCX allows you to leverage the DOCX format’s rich formatting and editing capabilities, making it ideal for creating professional, editable documents from e-book content. DOCX files are widely supported and easily shareable across different platforms and devices, ensuring accessibility and convenience.
Aspose.HTML for Python via .NET provides a simple and efficient way to perform this conversion using convert_epub() methods of the Converter class.
In this article, you find information on how to convert EPUB to DOCX using Aspose.HTML Python library and how to apply DocSaveOptions.
To continue following this tutorial, install and configure the Aspose.HTML for Python via .NET in your Python project.
Online EPUB Converter
Convert EPUB files to other formats in real-time using Aspose.HTML for Python via .NET. The static methods of the Converter class are primarily used as the easiest way to convert an EPUB file into various formats. Simply load an EPUB file from your local drive or a URL and run the conversion example with default save options. The result will be instantly available as a separate file.
If you want to convert EPUB to DOCX using DocSaveOptions
programmatically, please see the following Python code example.
Convert EPUB to DOCX
With Aspose.HTML for Python via .NET, you can convert EPUB to DOCX format programmatically with full control over a wide range of conversion parameters. To convert EPUB to DOCX with DocSaveOptions
specifying, you should follow a few steps:
- Open an existing EPUB file.
- Create a new DocSaveOptions object and specify the required save options.
- Use one of the convert_epub() methods to save EPUB as a DOCX file.
The following example shows how to use DocSaveOptions and create a DOCX file with custom save options:
1import os
2from aspose.html.converters import *
3from aspose.html.saving import *
4from aspose.html.drawing import *
5
6# Setup directories and define paths
7output_dir = "output/"
8input_dir = "data/"
9if not os.path.exists(output_dir):
10 os.makedirs(output_dir)
11document_path = os.path.join(input_dir, "input.epub")
12save_path = os.path.join(output_dir, "output.docx")
13
14# Open an existing EPUB file for reading
15with open(document_path, "rb") as stream:
16
17 # Create an instance of DocSaveOptions
18 options = DocSaveOptions()
19 options.page_setup.any_page = Page(drawing.Size(680, 500), Margin(10, 10, 10, 10))
20 options.font_embedding_rule.FULL
21 options.document_format.DOCX
22 options.css.media_type.SCREEN
23
24 # Convert EPUB to DOCX
25 Converter.convert_epub(stream, options, save_path)
In the example, we open and read the source file from the file system at the specified path. The DocSaveOptions()
constructor initializes an instance of the DocSaveOptions class that is passed to convert_epub()
method that takes the stream
, options
, output file path save_path
and performs the conversion operation. The DocSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting EPUB to DOCX format. In the example, we use the page_setup
, document_format
, font_embedding_rule
, and css.media_type
properties.
Note: You need to specify the path to the source and output file in your local file system (document_path
and save_path
).
Save Options – DocSaveOptions Class
Aspose.HTML for Python via .NET allows converting EPUB to DOCX using default or custom save options.
DocSaveOptions usage enables you to tune the rendering process. Some properties of this class inherit properties of base classes, such as
DocRenderingOptions or RenderingOptions. DocSaveOptions
is configured to save the document as DOCX and it includes the following properties:
- page_setup – This property lets you define the page’s layout, including page size, margins, and other layout aspects, ensuring the output document matches the desired format.
- horizontal_resolution – This property sets or gets the horizontal resolution for internal images in pixels per inch. By default, it is 300 dpi. Higher resolutions can produce better rendering quality but larger file sizes. This property allows you to control the trade-offs between quality and file size.
- vertical_resolution – This property sets or gets the vertical resolution for internal images in pixels per inch. By default, it is 300 dpi. Similar to
horizontal_resolution,
this controls the vertical resolution of documents, affecting their clarity and overall size. - background_color – This property allows you to set the background color for the rendered output. If not set, the default background is transparent.
- css – This property gets a CssOptions object, which is used to configure CSS properties processing. For example, the
css.media_type
property specifies different styles for different media types, ensuring that the correct CSS rules are applied based on how the document is being rendered. - font_embedding_rule – This property sets the rule for embedding fonts and controls whether and how fonts are embedded in the output document. The default value is
NONE
. - document_format – This property sets the file format of the output document. The default is DOCX.
Download the Aspose.HTML for Python via .NET library to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.
Aspose.HTML offers a free online EPUB to DOCX Converter that converts EPUB to DOCX with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!