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

Free Support Forum - aspose.cloud

Python Script to Search and Replace Text in PDF Throws Internal Error

I’m getting this error:

Blockquote
/usr/local/bin/python3 “/Users/ibrun/Library/Mobile Documents/com~apple~CloudDocs/programming/pdf editor/main.py”
Traceback (most recent call last):
File “/Users/ibrun/Library/Mobile Documents/com~apple~CloudDocs/programming/pdf editor/main.py”, line 20, in
response = pdf_api.post_document_text_replace(remote_name, text_replace_list)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/asposepdfcloud/apis/pdf_api.py”, line 23961, in post_document_text_replace
(data) = self.post_document_text_replace_with_http_info(name, text_replace, **kwargs)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/asposepdfcloud/apis/pdf_api.py”, line 24040, in post_document_text_replace_with_http_info
return self.api_client.call_api(’/pdf/{name}/text/replace’, ‘POST’,
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/asposepdfcloud/api_client.py”, line 411, in call_api
return self.__call_api(resource_path, method,
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/asposepdfcloud/api_client.py”, line 206, in __call_api
raise error
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/asposepdfcloud/api_client.py”, line 189, in __call_api
response_data = self.request(method, url,
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/asposepdfcloud/api_client.py”, line 454, in request
return self.rest_client.POST(url,
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/asposepdfcloud/rest.py”, line 288, in POST
return self.request(“POST”, url,
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/asposepdfcloud/rest.py”, line 250, in request
raise ApiException(http_resp=r)
asposepdfcloud.rest.ApiException: (500)
Reason: Internal Server Error
HTTP response headers: HTTPHeaderDict({‘Date’: ‘Tue, 01 Feb 2022 23:19:56 GMT’, ‘Content-Type’: ‘application/json; charset=utf-8’, ‘Transfer-Encoding’: ‘chunked’, ‘Connection’: ‘keep-alive’, ‘Server’: ‘Kestrel’, ‘Strict-Transport-Security’: ‘max-age=2592000’})
HTTP response body: {“RequestId”:“49bb624a-3edb-430f-8eed-8cf21c2464a0”,“Error”:{“Code”:“internalError”,“Message”:“Object reference not set to an instance of an object.”,“Description”:“Operation Failed. Internal error.”,“DateTime”:“2022-02-01T23:19:56.1836017Z”,“InnerError”:null}}
Blockquote

My code is the next one:

import os
import asposepdfcloud
from asposepdfcloud.apis.pdf_api import PdfApi

pdf_api_client = asposepdfcloud.api_client.ApiClient(
app_key=“xxxxx”,
app_sid=‘xxxxx’)

pdf_api = PdfApi(pdf_api_client)
filename = ‘Binder1.pdf’
remote_name = ‘Brinder1Results3.pdf’

pdf_api.upload_file(remote_name,filename)

text_replace1 = asposepdfcloud.models.TextReplace(old_value=‘Name’,new_value=‘David Aviles’,regex=‘true’)
text_replace2 = asposepdfcloud.models.TextReplace(old_value=‘n_rut’,new_value=‘19…915-0’,regex=‘true’)
text_replace_list = asposepdfcloud.models.TextReplaceListRequest(text_replaces=[text_replace1])

response = pdf_api.post_document_text_replace(remote_name, text_replace_list)
print(response)
pdf_api.download_file(remote_name, ‘results/’ + remote_name)

Any ideas?

And, in the other hand, how I suppouse to download the file with the modifications done?

@gapCloser

I have tested the search and replace text in PDF feature using the latest version of Aspose.PDF Cloud SDK for Python and I am unable to notice any issue. Please check the following sample for reference, it also includes code to download the modified file from cloud storage.

Steps to Search and Replace Text in PDF with Python

  1. sing up with aspose.cloud and get Client Id and Client Secret
  2. Install Aspose.PDF Cloud SDK for Python package from PIP
  3. Create a script File for the code and import Aspose.PDF Cloud modules
  4. Upload your sample PDF file to the Cloud Storage
  5. Find and Replace occurrences of Text in PDF document
  6. Download output PDF file from cloud storage

Python Code to Find and Replace Text in PDF

import os
import asposepdfcloud
from asposepdfcloud.apis.pdf_api import PdfApi
from shutil import copyfile

# Get App key and App SID from https://cloud.aspose.com
pdf_api_client = asposepdfcloud.api_client.ApiClient(
    app_key='xxxxxxxxxxxxxxxxxxxx',
    app_sid='xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx')

pdf_api = PdfApi(pdf_api_client)
filename = '02_pages.pdf'
remote_name = '02_pages.pdf'
output_name= 'C:/Temp/' + remote_name
copied_file= '02_pages_new.pdf'
#upload PDF file to storage
pdf_api.upload_file(remote_name,filename)

#upload PDF file to storage
pdf_api.copy_file(remote_name,copied_file)

#Replace Text
text_replace1 = asposepdfcloud.models.TextReplace(old_value='origami',new_value='aspose',regex='true')
text_replace2 = asposepdfcloud.models.TextReplace(old_value='candy',new_value='biscuit',regex='true')
text_replace_list = asposepdfcloud.models.TextReplaceListRequest(text_replaces=[text_replace1,text_replace2])

response = pdf_api.post_document_text_replace(copied_file, text_replace_list)
print(response)

#download PDF file from storage
response_download = pdf_api.download_file(copied_file)
copyfile(response_download, output_name)
print(output_name)


I’m geeting the same errror running your code. What can I do?Captura de Pantalla 2022-02-02 a la(s) 15.36.11.png (177.6 KB)

@gapCloser

Please ensure you are using the latest version of Aspose.PDF Cloud SDK for Python. However, if you still face the issue, then please share your complete code including credentials(Client Id and Secret) using flag button at the end of my post.

@gapCloser

Thanks for sharing the sample code and PDF document. It looks like some PDF document specific issue. I have logged a ticket PDFCLOUD-2610 for further investigation and rectification. We will notify you as soon as it is resolved.

@gapCloser

Please note we resolved your above reported issue. Please use the latest version of Aspose.PDF Cloud SDK for Python, it will resolve the issue.