Convert Word to PNG Online using Python | Word DOC to PNG

Word to PNG
Word to PNG Converter | Convert Word to PNG online using Python

This article provides the details on how to convert Word to PNG using Python SDK. We know that Word documents including DOC, DOCX, DOT, ODT, etc. are widely used for official data sharing. In some institutions, they are deemed as the defacto standard for official communication but when viewing on various platforms, the unavailability of required fonts used within word documents may hamper the fidelity of the document. Therefore, one of the viable solutions is to render Word to PNG raster images. The reason we have selected the PNG format is that these images can be viewed on the majority of platforms without any particular application.

Word Conversion API

In order to create, edit, split, merge and compare popular Word documents, we are going to use Aspose.Words Cloud SDK for Python. The SDK also enables you to render Word doc to PNG format. It 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

We also need to create a free account by visiting Aspose.Cloud dashboard, so that you can manage your documents in cloud storage.

Convert Word to PNG using Python

In this section, we are going to discuss the steps on how to load Word documents from cloud storage and then convert DOCX to PNG format. The resultant file is also saved in the same storage. Please follow the steps given below:

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

DOC to PNG using cURL Commands

In this section, we are going to discuss the conversion of DOC to PNG using cURL commands through the command line terminal. Now the first step is to generate a personalized JWT access token as per your client credentials. Please execute the following command to generate the JWT token:

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Since we are going to use the GetDocumentWithFormat API call, it expects the input DOC file to be available in cloud storage. So we need to upload the DOC file to cloud storage using the following command

curl -v -X PUT "https://api.aspose.cloud/v4.0/words/storage/file/input.doc" \
-H  "accept: application/json" \
-H  "Authorization: Bearer <JWT Token>" \
-H  "Content-Type: multipart/form-data" \
-d {"fileContent":{c:\Users\shahbnay\Downloads\test_doc.doc}}

Now the final step is to initiate the conversion operation using GetDocumentWithFormat API call. This API accepts two mandatory parameters i.e. name (name of input DOC file) and format (resultant file format. PNG in this scenario).

curl -v -X GET "https://api.aspose.cloud/v4.0/words/test_multi_pages.docx?format=PNG&outPath=Resultant.png" \
-H  "accept: application/octet-stream" \
-H  "Authorization: Bearer <JWT Token>"

The sample word document used in the above example can be downloaded from test_multi_pages.docx and the output file can be downloaded from Resultant.png.

Conclusion

In this article, we have learned the steps to convert Word to PNG using Python code snippet. At the same time, we have also learned how we can perform the Word DOC to PNG conversion through cURL commands. Another exciting way of exploring the API capabilities is through the Swagger interface.

We sincerely suggest you explore the Programmers Guide section to further learn about other capabilities of the API and you can also download the complete source code of SDK from GitHub and customize it as per your requirements (it’s published under an MIT license).

Related Topics

We recommend you to visit the following articles to learn about: