We're sorry GroupDocsCloud doesn't work properply without JavaScript enabled.

Free Support Forum - groupdocs.cloud

Python Bulk Find and Replace in Word Document

Hi @tilal.ahmad hope you are doing great.
Just wanted to reach out to you regarding one of the challenge I am facing . I was successful in converting pdf to word using Group Docs. Now on the converted word doc I want to perform bulk find and replace action where I have a dictionary which tells which word needs to be replaced with what . Is there a way in Python in which I can do bulk find and replace in Word Doc .
Any help is appreciated.
Thanks a lot for your support

@Gitika_Srivastava

Please check the sample Python code in the following documentation of GroupDocs.Editor Cloud to find and replace in Word document. Hopefully, it will help you to accomplish the task.

1 Like

Hi @tilal.ahmad I tried using this code and while I am providing the path to my desktop to read the .docx file I am getting below error:
Can’t find file located at ‘C:\Users\rk\Desktop\FY22.docx’.

Can you help me out where exactly the issue is . I am using the code in same format as provided. Also at the same path I have the doc file present but still getting the error.
Thanks!

@Gitika_Srivastava

Please share your sample code, we will look into it and will guide you accordingly.

Hi @tilal.ahmad PFA the code that has been coded as of now .Find and Replace.docx (12.3 KB)
Dummy.docx (206.2 KB)

What the use case is: In the attached word doc the word ‘Name’ has to be replace with word ‘Nom’.

Can you please help me modifying the code accordingly . Also would really appreciate if you can look into this at the earliest as it has started impacting my work and I need to accomplish this task ASAP.

Thanks!

@Gitika_Srivastava

Please pay attention to the sample Python code in above shared documentation link. You need to pass the file path from your cloud storage. So please upload your source file to cloud storage first and then use the sample code for text replacement.

....
editApi = groupdocs_editor_cloud.EditApi.from_keys(app_sid, app_key)
fileApi = groupdocs_editor_cloud.FileApi.from_keys(app_sid, app_key)
 
# The document already uploaded into the storage.
# Load it into editable state
fileInfo = groupdocs_editor_cloud.FileInfo("WordProcessing/password-protected.docx", None, None, "password")
loadOptions = groupdocs_editor_cloud.WordProcessingLoadOptions()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"
loadResult = editApi.load(groupdocs_editor_cloud.LoadRequest(loadOptions))
....
....        

thanks @tilal.ahmad. Can you help me with the below error which I am getting post uploading the doc to cloud space.
Attached is the snippet of the error that I am getting.
Thanks!Error.JPG (25.8 KB)

@Gitika_Srivastava

I am unable to notice any issue while uploading a file to cloud storage. Please share your sample code with us for investigation.

# Create instance of the API
api = groupdocs_editor_cloud.FileApi.from_keys(app_sid, app_key)

try:
    request = groupdocs_editor_cloud.UploadFileRequest("Temp\\Sample.docx", "C:\\Temp\\Sample.docx")
    response = api.upload_file(request)
            
    print("Expected response type is FilesUploadResult: " + str(response))
except groupdocs_editor_cloud.ApiException as e:
    print("Exception when calling get_supported_file_formats: {0}".format(e.message))

@tilal.ahmad PFA the code .GroupDocs.docx (12.1 KB)

Please let me know what needs to be updated here to get the error resolved.

Thanks

@Gitika_Srivastava

As stated in the shared documentation link above if your input document is not uploaded to cloud storage then you need to upload it first. Following are the steps to edit a document using GroupDocs.Cloud Editor.

  1. Upload input document into cloud storage
  2. Load the document into editable representation in the cloud storage (HTML file and resources)
  3. Download HTML document (and resources, if needed) from storage
  4. Edit HTML document at client side
  5. Upload HTML document back into the storage
  6. Save the edited document into WordProcessing format in the storage
  7. Download saved document
editApi = groupdocs_editor_cloud.EditApi.from_keys(app_sid, app_key)
fileApi = groupdocs_editor_cloud.FileApi.from_keys(app_sid, app_key)

# Upload document to cloud storage
request = groupdocs_editor_cloud.UploadFileRequest("Dummy.docx", "C:\\Temp\\Dummy.docx")
    response = fileApi.upload_file(request)
 
fileInfo = groupdocs_editor_cloud.FileInfo("Dummy.docx", None, None, "password")
loadOptions = groupdocs_editor_cloud.WordProcessingLoadOptions()
loadOptions.file_info = fileInfo
....