|
| 1 | +# Import Libraries |
| 2 | +from pdf2docx import parse |
| 3 | +from typing import Tuple |
| 4 | +import groupdocs_conversion_cloud |
| 5 | + |
| 6 | + |
| 7 | +def convert_pdf2docx(input_file: str, pages: Tuple = None): |
| 8 | + |
| 9 | + if pages: |
| 10 | + pages = [int(i) for i in list(pages) if i.isnumeric()] |
| 11 | + result = parse(pdf_file=input_file, pages=pages) |
| 12 | + print("###### Conversion Complete #######") |
| 13 | + return result |
| 14 | + |
| 15 | + |
| 16 | +def cloud_convert(app_sid, app_key): |
| 17 | + convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(app_sid, app_key) # Create instance of the API |
| 18 | + file_api = groupdocs_conversion_cloud.FileApi.from_keys(app_sid, app_key) |
| 19 | + |
| 20 | + try: |
| 21 | + # upload source file to storage |
| 22 | + filename = input("Please enter the complete and correct file path of the required pdf: ") |
| 23 | + remote_name = filename |
| 24 | + output_name = remote_name.split('.')[0] + "docx" |
| 25 | + remote_name = filename |
| 26 | + strformat = 'docx' |
| 27 | + |
| 28 | + request_upload = groupdocs_conversion_cloud.UploadFileRequest(remote_name, filename) |
| 29 | + file_api.upload_file(request_upload) |
| 30 | + |
| 31 | +# Extract Text from PDF document |
| 32 | + settings = groupdocs_conversion_cloud.ConvertSettings() |
| 33 | + settings.file_path = remote_name |
| 34 | + settings.format = strformat |
| 35 | + settings.output_path = output_name |
| 36 | + request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings) |
| 37 | + response = convert_api.convert_document(request) |
| 38 | + print("Document converted successfully: " + str(response)) |
| 39 | + |
| 40 | + except groupdocs_conversion_cloud.ApiException as e: |
| 41 | + print("Exception when calling get_supported_conversion_types: {0}".format(e.message)) |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + |
| 46 | + choice = input("Press 1 for basic file conversion, 2 for advanced file conversion(GroupDocs account required)") |
| 47 | + while choice != '1' and choice != '2': |
| 48 | + print("Please enter a valid choice!\n") |
| 49 | + choice = input("Press 1 for basic file conversion, 2 for advanced file conversion(GroupDocs account required)") |
| 50 | + if choice == '1': |
| 51 | + |
| 52 | + pdf_path = input("Please enter the complete and correct file path of the required pdf: ") |
| 53 | + convert_pdf2docx(pdf_path) |
| 54 | + else: |
| 55 | + sid = input("Enter app SID: ") |
| 56 | + api_key = input("Enter api key: ") |
| 57 | + cloud_convert(sid, api_key) |
0 commit comments