How To Convert DOCX to PDF Without Word Installed in Python and Download Output?

Hello

I am using Aspose.Words Cloud SDK for Python for file conversion, and I can see it in the cloud storage.
How can I download the file, or even directly read the file with Python?

@JeffSartorius

You can use DownloadFile API to download files from cloud storage and here is Aspose.Words Cloud SDK for Python sample code for reference.

remoteFolder = 'Temp'
localFolder = 'C:/Temp'
remoteFileName= 'revisions.docx'

#download file from storage
request_download=asposewordscloud.models.requests.DownloadFileRequest(remoteFolder + '/' + remoteFileName)
response_download = words_api.download_file(request_download)
copyfile(response_download, localFolder + '/' +"output.docx")
1 Like

@tilal.ahmad
Thank you. It works, and the copyfile function is imported from shutil.

1 Like

@tilal.ahmad Thank you again. I have one more question:

How to use the DownloadFile API with Python? I donā€™t know how to construct the ā€œAuthorizationā€, the server always response [401 Authorization token is invalid or expired. ].

@JeffSartorius

Please find the complete sample code to convert DOCX to PDF without Word installed in Python and download output file from cloud storage using Aspose.Words Cloud API. Hopefully, it will help you to accomplish the task.

However, if you still face the issue then please share your sample code along with the credentials. Please share the credentials via a private message. For private message, click on my user icon and opt the message tab with the required information. Kindly remove ā€œRE:ā€ from the title as well. We will investigate the issue and will update you.

Steps to Convert DOCX or DOC to PDF Online Python Without Word

  • Signup with aspose.cloud to get credentials for free trial
  • Install Aspose.Words Cloud SDK for Python from PIP
  • Create script file and import asposewordscloud API
  • Initiate API with credentials
  • Upload input file to Cloud storage using ā€˜upload_fileā€™ API method
  • Convert DOCX to PDF with save_as API method
  • Finally download output PDF from Cloud storage to local drive

Sample Python Code to Convert DOCX to PDF Online Without Word

# For complete examples and data files, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-python
# Import module
import asposewordscloud
import asposewordscloud.models.requests
from shutil import copyfile

# Please get your Client ID and Secret from https://dashboard.aspose.cloud.
client_id = 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx'
client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

# Initiate API
words_api = asposewordscloud.WordsApi(client_id,client_secret)
words_api.api_client.configuration.host='https://api.aspose.cloud'

inputFileName = 'C:/Temp/02_pages.docx'
remoteFileName = 'DocumentSaveAs.docx'
outputFileName = 'DocumentSaveAs.pdf'
localFolder = 'C:/Temp'

#upload input file to storage
request_upload = asposewordscloud.models.requests.UploadFileRequest(open(inputFileName,'rb'),remoteFileName)
response_upload = words_api.upload_file(request_upload)

#Convert DOCX to PDF and save to storage
save_options = asposewordscloud.SaveOptionsData(save_format='pdf', file_name=outputFileName)
request_conversion = asposewordscloud.models.requests.SaveAsRequest(remoteFileName, save_options)
response_conversion = words_api.save_as(request_conversion)
print(response_conversion)

#download file
request_download=asposewordscloud.models.requests.DownloadFileRequest(outputFileName)
response_download = words_api.download_file(request_download)
copyfile(response_download, localFolder + '/' + outputFileName)