Download DOCX to PDF converted file from cloud storage to disk using Python API

Hi, I want to convert my pdf files to docx. I followed the tutorials and successed to converted the files but I can’t download them. When I use convert_api.convert_document_download(request) function, it returns a path in temp directory on my computer but the converted file is not there.

@soilprime

We are sorry for your inconvenience. Please note, the file_api.download_file method writes response body into a temp file and returns file path. You may copy file to desired path as following. Hopefully it will help you to accomplish the task.

# Import module
import groupdocs_conversion_cloud

from shutil import copyfile

# Get your app_sid and app_key at https://dashboard.groupdocs.cloud (free registration is required).
app_sid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
app_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Create instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(app_sid, app_key)
file_api = groupdocs_conversion_cloud.FileApi.from_keys(app_sid, app_key)

try:

        #upload soruce file to storage
        filename = '02_pages.docx'
        
        request_download = groupdocs_conversion_cloud.DownloadFileRequest(filename)
        response_download = file_api.download_file(request_download)
        copyfile(response_download, 'C:/Temp/02_pages_copy.docx')

except groupdocs_conversion_cloud.ApiException as e:
        print("Exception : {0}".format(e.message))
1 Like

Thanks! I am getting the file downloaded in my folder but i am unable to read the file. It is showing an error which i have attached here. Kindly help me on this.Error.PNG (3.7 KB)

@Sai_gopal

Thanks for your inquiry. Please double check the file format of source document on the cloud storage and downloaded document is same. However, If the issue persists then please share your source document and code here. We will look into it and will guide you accordingly.

Hi Tilal, Thanks for your early response!!
I am getting the pdf converted into docx and I can open it in my dashboard
but when I go through the codes to download the response file into my system it is coming out as sample.docx (137 kb)but i am getting the pdf file again as i am able to open it through the ADOBE reader. I am sharing the source document and codes. Kindly resolve the issue.

Sruthi_Sekaran_Resume_28July2019.pdf (136.4 KB)

Python code:

Import module

import groupdocs_conversion_cloud
import shutil

Get your app_sid and app_key at https://dashboard.groupdocs.cloud (free registration is required).

app_sid = “xxxx-xxxx-xxxx-xxxx”
app_key = “xxxxxxxxxxxxxxxx”

Create instance of the API

convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(app_sid, app_key)
file_api = groupdocs_conversion_cloud.FileApi.from_keys(app_sid, app_key)

try:

    #upload soruce file to storage
    filename = 'Sruthi_Sekaran_Resume_28July2019.pdf'
    remote_name = 'Sruthi_Sekaran_Resume_28July2019.pdf'
    output_name= 'sample.docx'
    strformat='docx'

    request_upload = groupdocs_conversion_cloud.UploadFileRequest(remote_name,filename)
    response_upload = file_api.upload_file(request_upload)
    # Convert PDF to Word document
    settings = groupdocs_conversion_cloud.ConvertSettings()
    settings.file_path = remote_name
    settings.format = strformat
    settings.output_path = output_name

    loadOptions = groupdocs_conversion_cloud.PdfLoadOptions()
    loadOptions.hide_pdf_annotations = True
    loadOptions.remove_embedded_files = False
    loadOptions.flatten_all_fields = True

    settings.load_options = loadOptions

    convertOptions = groupdocs_conversion_cloud.DocxConvertOptions()
    convertOptions.from_page = 1
    convertOptions.pages_count = 1

    settings.convert_options = convertOptions

    request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
    response = convert_api.convert_document(request)

    request_download = groupdocs_conversion_cloud.DownloadFileRequest(filename)
    response_download = file_api.download_file(request_download)
    shutil.copyfile(response_download, 'D://Temp//Folder//sample.docx')

    print("Document converted successfully: " + str(response))

except groupdocs_conversion_cloud.ApiException as e:
print(“Exception when calling get_supported_conversion_types: {0}”.format(e.message))

Hi,
I got it.
The code should be
request_download = groupdocs_conversion_cloud.DownloadFileRequest(output_name)
instead of
request_download = groupdocs_conversion_cloud.DownloadFileRequest(filename)
Thanks Tilal

@Sai_gopal

Thanks for your feedback. It is good to know that you have manged to resolve the issue at your end. Please keep using our API and feel free to contact us for any assistance.

1 Like

Hi @tilal.ahmad, I was just going through this code on how to download a converted file from GroupDocs. So while giving the folder’s path where file has to be downloaded , the code is throwing the below error :
PermissionError: [Errno 13] Permission denied: "C:\Users\rkargwal\Desktop .

Can you please help how this can be resolved. Thanks in advance

Hi @tilal.ahmad the above issue is resolved , I guess what I was giving as an output path was the directory name and not the file name. When I tried giving the complete path with the file name the output was there. However I do not want a single file to be downloaded at my desktop folder, I want a bulk download of .docx file. How can I do that?

@Gitika_Srivastava

I am afraid there is no option for bulk download. You need to call DownloadFile API for each file download separately. You may get the files list from a folder using GetFilesList API method and then download the files in a loop.

thanks @tilal.ahmad… Also can you help me out with the API calls. How API’s are getting calculated while using GroupDocs. I am planning to buy GroupDocs API’s but not sure how much API should I buy. I tried uploading a PDF and downloading a word doc and it cost me 2 API’s. Similarly if I need to upload a pdf ,then convert it , download it to my local path and then delete it from GroupDoc environment.How much API would that cost. Please let me know how API’s are calculated.
I also had concern with the data security. Is my data secured since I am using my organization data, is my data secured when I am uploading it and converting it.I am as of now using GroupDocs internal storage. What would you recommend here
Thanks in advance!

@Gitika_Srivastava

Please check following pricing page. Our pricing plan is quite flexible, your first 150 API calls will be free and then you will be charged as much as you use. Please note we have two types of Cloud APIs; one hosted at groupdocs.cloud servers and other self-hosted API using Docker Container. The pricing plan of both are same except we do not charge storage API in self hosting.

Secondly, you can use ConvertDocumentDirect API method to convert documents without cloud storage with a single API call.

We take security very seriously. Please check our security policy page, it includes data security policy details along with other related policies.

Security - About - groupdocs.cloud

thanks for the details information @tilal.ahmad. Appreciate it…
You just told its possible to convert documents without cloud storage with a single API call. I need something like this where I can convert pdf to word without using internal storage… If possible can you share some sample/actual code in python that can make this happen. It would great help to me.
Thanks again!

@Gitika_Srivastava

Please find the sample code for converting PDF to DOCX without cloud storage. Hopefully, it will help you to accomplish the task. Please also check the following documentation for details.

# Import module
import groupdocs_conversion_cloud
from shutil import copyfile

# Get your client_id and client_key at https://dashboard.groupdocs.cloud (free registration is required).
client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx"
client_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Create instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_key)

try:

        #Convert PDF to DOCX
        # Prepare request
        request = groupdocs_conversion_cloud.ConvertDocumentDirectRequest("docx", "Book1_LoadOptions.pdf")

        # Convert
        result = convert_api.convert_document_direct(request)       
        copyfile(result, 'output.docx')
        print("Result {}".format(result))
        
except groupdocs_conversion_cloud.ApiException as e:
        print("Exception when calling get_supported_conversion_types: {0}".format(e.message))

awesome… Thanks for the help @tilal.ahmad. If in case I face some issue I’ll get back to you. Thanks again!

1 Like

A post was split to a new topic: How to Convert Excel to Word Document in Python

A post was split to a new topic: Python Page formatting issue in PDF to Word Document Conversion