Make Impressive Word to JPG Converter using Python

Learn how to Convert Word to JPG online. Create an awe-inspiring Word to Image converter with fewer Python code lines

Convert Word to JPG
Convert Word to JPG | Save Word into Image using Python SDK

In this article, we are going to discuss the details on how to convert Word to JPG using Python SDK. For conversion purposes, we will be using Cloud SDK which is a wrapper around our REST API for MS Word documents processing.

Word Document Processing API

Aspose.Words Cloud is our REST API offering the capabilities to create, edit, manipulate and render MS Word files to XPS, PDF, HTML, JPEG, PNG, and other supported file formats. It’s capable of loading MS Word (DOCX, DOC, DOT, RTF, DOCM) or OpenDocument (ODT, OTT) format without MS Word or MS Office automation. In this article, we are going to focus on document conversion using Python, so we need to use Aspose.Words Cloud SDK for Python, which is a wrapper around Cloud API.

Installation

The SDK is available for download at PIP and GitHub. Execute the following command on the command line terminal to install the SDK

pip install aspose-words-cloud

Convert Word to JPG in Python

In order to get started with Cloud APIs, we need to create an account on Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up or, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Client Credentials
Image 1:- Client Credentials preview

Aspose.Cloud dashboard provides a single interface to manage various file storages, so you may connect Amazon S3, DropBox, Google Drive Storage, Google Cloud Storage, Windows Azure Storage, and FTP Storage. We can manage these sources using the Storages option available in the left menu on the dashboard.

Various storage options
Image 2:- Various storage options on Cloud dashboard.

Load Word Document from Cloud Storage

In this section, we are going to discuss the steps on how to load Word document storage in default cloud storage and convert the output to JPEG format. The resultant file is also saved in the same storage. In this example, we need to configure the Internal Storage option (the first option as shown in the above image). Please follow the steps given below to perform the conversion operation.

  • Firstly, create an instance of the WordsApi object while passing ClientID and ClientSecret details as arguments
  • Secondly, upload PDF file to Cloud storage using UploadFileRequest(..) method
  • Thirdly, create an object of GetDocumentWithFormatRequest object while passing input Word file name, desired output format, and resultant file name as arguments
  • Finally, call the get_document_with_format(..) method of WordsApi class to perform the conversion operation
Word to JPEG preview
Image 3:- Word to JPEG conversion preview.

Word Document from Google Drive

In this section, we will be discussing the details of how to integrate the Google Drive account with Aspose.Cloud dashboard and then Word document will be loaded from same Google Drive. After the conversion, the resultant JPEG will also be saved to the same drive.

So first step is to link the Google Drive account with your Aspose.Cloud Dashboard account.

  • Click the Storages option from the left menu on the dashboard
  • Click Create New Storage button on the Right-Bottom of the page
  • Select the Google Drive Storage option from the menu
  • Enter Storage name i.e. GDrive
  • Enter Client ID and Client Secret details from the Google API console
  • Then click Generate Refresh Token button and authorize the Google account where the Cloud project is created. Once all details are provided, the screen will appear as shown below
Google Drive credentials
Image 4:- Preview of Google Drive storage credentials.

We recommend visiting following link to Learn More About Connecting 3rd Party Storage Providers.

We only interact with Aspose.Cloud APIs using Application created on Aspose.Cloud dashboard and at the backend, each application has storage connected with it. So now we need to update the Storage information for the existing Application.

Storage update on Cloud dashboard
Image 5:- Update Storage details on the Cloud dashboard.

Execute the code shared above and it will convert the Word document available in Google Drive to JPEG format and save it to the same storage.

Word Document from Dropbox Storage

We can also configure our Dropbox storage with a cloud application and can easily convert the Word files stored in Dropbox storage to JPEG format.

  • Click the Storages option from the left menu on Aspose.Cloud dashboard
  • Click Create New Storage button and select Dropbox Storage from the menu
  • Enter the name of your choice in the Storage Name field and click Generate Token button
  • You will be prompted to provide your Dropbox account details and upon successful authentication, the access token will be generated
  • Click the Save button
Dropbox storage details
Image 6:- Dropbox storage details.

Again we need to update the storage details for the Cloud application that we are accessing in our code (through Client ID and Client credentials). Now click the Files option from the left menu on the dashboard and select Dropbox Storage (name of storage provide above) and all the files listed on Dropbox will be displayed.

Dropbox file on Cloud Dashboard
Image 7:- Dropbox files appearing on Cloud Dashboard.

At the code level, we do not need to make any change and when the above-shared code snippet is executed, a new Word document is uploaded to Dropbox storage and the resultant JPEG is also saved in the same storage. See the image shown below.

DOC converted to JPEG in Dropbox
Image 8:- Preview of Word file converted to JPEG in Dropbox.

Save Word into Image using cURL Command

The cURL commands are one of the convenient ways of accessing REST APIs through command prompt. Since Aspose.Words Cloud is also developed as per REST architecture, so we can also access it via cURL commands. We know that Aspose APIs are only accessible to authorized persons only, so we need to generate JWT access tokens based on client credentials. Please execute the following command to generate one:

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=6185429e-17ce-468d-bb81-a51ac9d96c16&client_secret=73a07e2fb010f559e482d854fe5a8f49" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now execute the following cURL command to convert Word document already available in Cloud storage to JPEG format. The resulting file is also stored in the same cloud storage.

curl -X GET "https://api.aspose.cloud/v4.0/words/source.doc?format=JPEG&outPath=Converted.jpeg" \
-H  "accept: application/octet-stream" \
-H  "Authorization: Bearer <JWT Token>"

Since Dropbox is linked as current storage with Aspose.Cloud API, so source.doc is loaded from Dropbox and the resultant JPEG is also saved on the same storage.

Preview of output on Dropbox
Image 9:- Preview of output on Dropbox.

Conclusion

In this article, we have explored the capabilities of Aspose.Words Cloud on how to convert Word to JPG using Python. We have also discussed the steps for taking leverage of various Cloud storage available including Google Drive and Dropbox. We have also explored the option of how to save a word document as a jpeg using cURL commands. Please note that our SDKs are developed as per an MIT license, so their complete source code is available for download over Github. In case you encounter any issues or you have any further queries, please feel free to contact us via the Free product support forum.

Related Articles

We recommend you visit the following links to learn more about