Python is among the popular programming languages and in this article, we are going to discuss very simple steps on how to convert Word to PDF in Python application. The whole conversion is performed in the Cloud without using MS Office automation or any other software installed.
- Word to PDF Conversion API
- Installation and configuration
- Word to PDF in Python
- The cURL approach for conversion
Word to PDF Conversion API
Aspose.Words Cloud is a REST-based API offering the capabilities to read, create, edit, merge, split as well as convert, render and perform a wide variety of other operations on Word documents in the Cloud. In this article, we are further going to discuss Word to PDF conversion using Aspose.Words Cloud SDK for Python. So the first step in using the Cloud SDK is its installation. It is available for download on PIP and GitHub, so please execute the following command to perform the installation from pip.
pip install aspose-words-cloud
In case you are using PyCharm IDE for development purposes, simply search asposewordscloud on the Python Interpreter window under PyCharm ->Preferences… section.
The next important step is the creation of a free account over Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button. Obtain your personalized Client Credentials that will be used in the subsequent sections.
Word to PDF in Python
The conversion of MS Word files (DOC, DOCX, etc) into a PDF format can be accomplished by following the steps given below.
- The first step is to create an ApiClient object which takes Client ID and Client Secret details as arguments
- Secondly, initialize an instance of the WordsApi object which takes the ApiClient object as an argument
- Thirdly, call the upload_file(…) method of WordsApi class to upload the file to cloud storage
- The next step is to create a ConvertDocumentRequest instance which takes the input file name, output file format, and resultant file name as arguments
- Finally, call the convert_document(…) method to perform the conversion operation
The cURL approach for conversion
The cURL commands are one of the smartest approaches to access REST APIs through command prompt. So in this section, we are going to convert Word to PDF using cURL Commands. Now as a pre-requisite, the first step is to generate JSON Web Token (JWT) which is used to access the APIs through cURL commands.
Please execute the following command to generate a JWT token based on provided Client ID and Client Secret. You may consider visiting the following link for more information on How to Obtain JWT token using a Client ID and Client Secret key.
curl -v "https://api.aspose.cloud/connect/token" \ -X POST \ -d "grant_type=client_credentials&client_id=718e4235-8866-4ebe-bff4-f5a14a4b6466&client_secret=388e864b819d8b067a8b1cb625a2ea8e" \ -H "Content-Type: application/x-www-form-urlencoded" \ -H "Accept: application/json"
Once the JWT token has been generated, access GetDocumentWithFormat API to perform DOC to PDF conversion.
curl -X GET "https://api.aspose.cloud/v4.0/words/GetStyles.doc?format=pdf&outPath=Resultant.pdf" \ -H "accept: application/octet-stream" \ -H "Authorization: Bearer <JWT Token>"
In this article, we have discussed an exciting approach of converting Word to PDF using Python code snippet. You may perform the conversion of MS Word documents including but not limited to (DOC, DOCX, etc) formats. Furthermore, please note that you may also convert Word documents to other formats including PDF, XPS, SVG, PCL, JPEG, and much more. For more information, please visit the Overview section.
In case you encounter any issues while using our cloud services, please contact us via the Free product support forum.
We also recommend you visit the following links for related information on other features being offered by Cloud APIs