Word to JPEG – Convert Word to JPEG in Python

Word to JPEG

In this article, we are going to discuss the conversion of DOC files to JPEG format 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 file 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 JPEG 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, 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 available on Dashboard.

Word file in 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 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 resultatn 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 in Google Drive

In this section, we will be discussing the details on how to integrate 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 same drive.

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

  • Click the the Storages option from left menu on dashboard
  • Click Create New Storage button on Right-Bottom of page
  • Select Google Drive Storage option from menu
  • Enter Storage name i.e. GDrive
  • Enter Client ID and Client Secret details from Google API console
  • Then click Generate Refresh Token button and authroize the Google account where 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 existing Application.

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

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

Document in Dropbox

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

  • Click Storages option from left menu on Aspose.Cloud dashboard
  • Click Create New Storage button and select Dropbox Storage from menu
  • Enter name of your choice in Storage Name field and click Generate Token button
  • You will be prompted to provide your Dropbox account details and upon successful authentication, 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 Cloud application that we are accessing in our code (through Client ID and Client credentials). Now click the Files option from left menu on 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 code level, we do not need to make any chnage and when above shared code snippet os executed, a new Word document is uploaded to Dropbox storage and the resultant JPEG is also saved in same storage. See the image shown below.

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

Word to JPEG 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 geneate JWT access token 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 result file is also stored in 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 resultant JPEG is also saved on 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 for Word document conversion to JPEG format. We have also discussed the steps for taking leverage of various Cloud storages available including Google Drive and Dropbox. Please note that our SDKs are developed as per MIT license, so their complete source code is available for download over Github. In case you encounter any issue or you have any further query, please feel free to contact us via Free product support forum.

Related Articles

We recommend you to visit following links to learn more about