KNOWLEDGEBASE
Python .docx tutorial | Create, open, and convert .docx file
In this article, we’ll show you how to easily create, open, or convert .docx files using Python!
We will introduce you to the Aspose.Words for Python via .NET library, its functionalities, and features so that you can manipulate your .docx files in a quick and easy way.
Aspose.Words for Python via .NET is a robust and powerful API designed to read and manipulate documents of various types, like DOCX, DOC, PDF, RTF, DOTX, DOT, DOCM, DOTM, FlatOPC, FlatOpcMacroEnabled, ODT, XML, XAML, HTML, MHTML, TXT, XPS, PS, PNG, JPEG, BMP, SVG, EMF, GIF, EPUB, and other file formats.
Our library contains more than 100 Python classes to enable developers a unique opportunity to implement script-based document automation.
Aspose.Words for Python via .NET provide working examples and demos to help developers in the development process and boasts a rich feature set, platform independence, independence from third-party applications (such as Microsoft Word), performance and scalability, and a minimal learning curve.
Some of the advanced features of our API:
• converting documents between several popular formats
• rendering pages with high fidelity
• comparing documents
• designing Microsoft Word reports using mail merge fields
• cloning documents
• splitting documents into parts
• finding and replacing text
• working with digital signatures
• working with tables
• working with watermarks
• and more features that you can find in our documentation
Below, we’ll show you how to create a new or load an existing document, as well as how to convert your document using Python.
Create a new .docx file using Python
To create a new document, we’ll first call the Document constructor with no parameters, and use the DocumentBuilder class to add document contents.
In the code example below, we will show you how to create a document using the document builder:
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.write("Hello world!")
doc.save(docs_base.artifacts_dir + "out.docx")
Load .docx file using Python
To load an existing .docx file pass the file name or the stream into one of the Document constructors. The format is automatically determined by its extension.
Load from a file
To open an existing .docx file, pass the file name as a string to the Document constructor.
In the code example below you can see how to open a document from a file:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Document.docx")
Load from a stream
Pass a stream object that contains the document into the Document constructor to open a document from a stream.
In the code example below you can see how to open a document from a stream:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
# Read only access is enough for Aspose.words to load a document.
stream = io.FileIO(docs_base.my_dir + "Document.docx")
doc = aw.Document(stream)
# You can close the stream now, it is no longer needed because the document is in memory.
stream.close()
The template file for this example is available for download on the Aspose.Words GitHub.
Convert .docx file using Python
Easily and reliably converting documents from one format to another is one of the main features of Aspose.Words API. Conversion from a .docx format to another one is itself very simple, and can be accomplished with just two steps:
• The first step is loading a file into a Document object using one of its constructors.
• The second step is to invoke one of the save methods on the Document object and specify the desired output format.
You can convert a .docx file to multiple formats using our API, and in the following example, we will show you how to convert a .docx file to PDF format.
Convert .docx file to PDF
In the code example below we will show you how to convert a .docx file into a PDF using the save method:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Document.docx")
doc.save(docs_base.artifacts_dir + "BaseConversions.docx_to_pdf.pdf")
The template file for this example is available for download on the Aspose.Words GitHub.
Converting to various PDF standards
Our library provides the PdfCompliace enumeration to support the conversion of .docx files into various PDF format standards (such as PDF 1.7, PDF 1.6, etc.).
In the code example below you can see how to convert a .docx file to PDF 1.7 using PdfSaveOptions with compliance with PDF17:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Rendering.docx")
saveOptions = aw.saving.PdfSaveOptions()
saveOptions.compliance = aw.saving.PdfCompliance.PDF17
doc.save(docs_base.artifacts_dir + "WorkingWithPdfSaveOptions.conversion_to_pdf_17.pdf", saveOptions)
You can try our free online converter and familiarize yourself with the functionalities of our API.