While working with document conversion feature, you came across a requirement to convert PDF to DOCX. I would like to introduce GroupDocs.Conversion Cloud SDK for Python for the purpose. It can also convert all popular industry standard documents from one format to another without depending on any third-party tool or software.
All you need to convert PDF to DOCX in Python follow these steps:
Before we begin with coding, sign up with groupdocs.cloud to get your APP SID and APP Key.
Install groupdocs-conversion-cloud package from pypi with the following command.
>pip install groupdocs-conversion-cloud
- Open your favorite editor and copy paste following code into the script file
- Import the GroupDocs.Conversion Cloud Python package
- Initialize the API
- Upload source PDF document to GroupDocs default storage
- Convert the PDF document to editable DOCX
# Import module
import groupdocs_conversion_cloud
# Get your app_sid and app_key at https://dashboard.groupdocs.cloud (free registration is required).
app_sid = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"
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 = 'Sample.pdf'
remote_name = 'Sample.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)
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))
- And that’s it. PDF document is converted to DOCX and API response includes the URL of the resultant document. Read more.
Top comments (3)
Hello @tilalahmad ! I fail to execute the above code correctly. I've added the app_sid and app_key to the code as per my created test.pdf app on groupdocs. This error is coming when I run python3 filename.py. It does give that the document is converted successfully and when I go to the url, there's an error. I'm sharing the ss of the same
@medhagupta098
If your conversion is successful then either you can view/download your file from cloud storage using dashboard.groupdocs.cloud or download file to local drive as follows:
Unable to attach the ss