def pdf2doc(file, filename, output_file, output_name): try: # upload soruce file to storage remote_name = file strformat = 'doc' 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() # 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 = 10 settings.convert_options = convertOptions request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings) response = convert_api.convert_document(request) print("Document converted successfully: " + str(response)) # upload source file to storage request_download = groupdocs_conversion_cloud.DownloadFileRequest( output_name) response_download = file_api.download_file(request_download) copyfile(response_download, f"{output_file}") except groupdocs_conversion_cloud.ApiException as e: print("Exception when calling get_supported_conversion_types: {0}". format(e.message)) return render_template("index.html", script=f'<script>x=null</script>')
def Run(self): # Create instance of the API api = Common_Utilities.Get_ConvertApi_Instance() try: settings = groupdocs_conversion_cloud.ConvertSettings() settings.storage_name = Common_Utilities.myStorage settings.file_path = "converted/topdf/password-protected.pdf" settings.format = "docx" loadOptions = groupdocs_conversion_cloud.PdfLoadOptions() loadOptions.password = "******" 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 settings.output_path = "converted\\todocx" request = groupdocs_conversion_cloud.ConvertDocumentRequest( settings) response = api.convert_document(request) print("Document converted successfully: " + str(response)) except groupdocs_conversion_cloud.ApiException as e: print("Exception while calling API: {0}".format(e.message))
def Run(cls): # Create necessary API instances apiInstance = groupdocs_conversion_cloud.ConvertApi.from_config( Common.GetConfig()) # Prepare convert settings settings = groupdocs_conversion_cloud.ConvertSettings() settings.file_path = "Pdf/sample.pdf" settings.format = "docx" loadOptions = groupdocs_conversion_cloud.PdfLoadOptions() loadOptions.password = "" loadOptions.hide_pdf_annotations = True loadOptions.remove_embedded_files = False loadOptions.flatten_all_fields = True settings.load_options = loadOptions settings.convert_options = groupdocs_conversion_cloud.DocxConvertOptions( ) settings.output_path = "converted" # Convert result = apiInstance.convert_document( groupdocs_conversion_cloud.ConvertDocumentRequest(settings)) print("Document converted: " + result[0].url)
def pdfToDocx(filename, remote_name, output_name): app_sid = api_sid app_key = api_key print("KEYS accepted") # no. of pages in pdf file print(app.config['UPLOAD_FOLDER'] + remote_name) file = PdfFileReader(open(app.config['UPLOAD_FOLDER'] + remote_name, 'rb')) page_counts = file.getNumPages() print("Number of pages: ", page_counts) print("Download path: ", app.config['DOWNLOAD_FOLDER'] + output_name) # 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 = filename remote_name = remote_name output_name = remote_name.rsplit('.')[0] + '.docx' strformat = 'docx' print(filename, remote_name, output_name) request_upload = groupdocs_conversion_cloud.UploadFileRequest( remote_name, filename) print("upload request") response_upload = file_api.upload_file(request_upload) print("uploaded to the cloud") #Convert PDF to Word document settings = groupdocs_conversion_cloud.ConvertSettings() settings.file_path = remote_name settings.format = strformat settings.output_path = output_name print("converted to docx") 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 = int(page_counts) settings.convert_options = convertOptions request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings) response = convert_api.convert_document(request) print("Document converted successfully: " + str(response)) # Download request request_download = groupdocs_conversion_cloud.DownloadFileRequest( output_name) response_download = file_api.download_file(request_download) print("Response Download ", response_download) copyfile(response_download, app.config['DOWNLOAD_FOLDER'] + output_name) print("Successful") except groupdocs_conversion_cloud.ApiException as e: print("Exception when calling get_supported_conversion_types: {0}". format(e.message)) return render_template("api_exception.html")
filename = '02_pages.pdf' remote_name = '02_pages.pdf' output_name = 'sample.docx' strformat = 'docx' request_upload = groupdocs_conversion_cloud.UploadFileRequest( path_pdf, path_pdf) response_upload = file_api.upload_file(request_upload) # Convert PDF to Word document settings = groupdocs_conversion_cloud.ConvertSettings() settings.file_path = path_pdf 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 = 2 #convertOptions.pages_count = 2 settings.convert_options = convertOptions settings.output_path = "converted\\todocx" request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings) response = convert_api.convert_document(request)[0].url