EPUB, short for Electronic Publication, is a widely used standard format for eBooks. It is an open and free format based on web standards, enabling compatibility with various devices and software applications. EPUB files are designed to provide a consistent reading experience across different platforms, including e-readers, tablets, smartphones, and computers. By converting your Word document to EPUB, you can ensure that your content is accessible and enjoyable to a broader audience, regardless of the devices and software they use. In this article, we will demonstrate how to convert Word documents to EPUB format in Python using Spire.Doc for Python.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Convert Word to EPUB in Python
The Document.SaveToFile(fileName:str, fileFormat:FileFormat) method provided by Spire.Doc for Python supports converting a Word document to EPUB format. The detailed steps are as follows.
- Create an object of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Save the Word document to EPUB format using Document.SaveToFile(fileName:str, fileFormat:FileFormat) method.
- Python
from spire.doc import * from spire.doc.common import * # Specify the input Word document and output EPUB file paths inputFile = "Sample.docx" outputFile = "ToEpub.epub" # Create an object of the Document class doc = Document() # Load a Word document doc.LoadFromFile(inputFile) # Save the Word document to EPUB format doc.SaveToFile(outputFile, FileFormat.EPub) # Close the Document object doc.Close()
Convert Word to EPUB with a Cover Image in Python
Spire.Doc for Python enables you to convert a Word document to EPUB format and set a cover image for the resulting EPUB file by using the Document.SaveToEpub(fileName:str, coverImage:DocPicture) method. The detailed steps are as follows.
- Create an object of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Create an object of the DocPicture class, and then load an image using DocPicture.LoadImage() method.
- Save the Word document as an EPUB file and set the loaded image as the cover image of the EPUB file using Document.SaveToEpub(fileName:str, coverImage:DocPicture) method.
- Python
from spire.doc import * from spire.doc.common import * # Specify the input Word document and output EPUB file paths inputFile = "Sample.docx" outputFile = "ToEpubWithCoverImage.epub" # Specify the file path for the cover image imgFile = "Cover.png" # Create a Document object doc = Document() # Load the Word document doc.LoadFromFile(inputFile) # Create a DocPicture object picture = DocPicture(doc) # Load the cover image file picture.LoadImage(imgFile) # Save the Word document as an EPUB file and set the cover image doc.SaveToEpub(outputFile, picture) # Close the Document object doc.Close()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.