Search code examples
pythonpdfasposeaspose.pdf

Replacing Text in a PDF with Aspose PDF Cloud using Python


I've been trying to replace text from a PDF file using Aspose PDF Cloud

!pip install asposepdfcloud

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

pdf_api_client = asposepdfcloud.api_client.ApiClient(
    app_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    app_sid='xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx')

pdf_api = PdfApi(pdf_api_client) 
filename = '/content/sample_data/dummy.pdf' 
remote_name = '/content/sample_data/dummy.pdf'

pdf_api.upload_file(remote_name,filename) 

text_replace1 = asposepdfcloud.models.TextReplace(old_value='PDF',new_value='XXX',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)

However, when I try running the above code it yields an error

ApiException: (404)
Reason: Not Found
HTTP response headers: HTTPHeaderDict({'Date': 'Mon, 08 Mar 2021 08:21:44 GMT', 'Content-Length': '0', 'Connection': 'keep-alive', 'Server': 'Kestrel', 'Strict-Transport-Security': 'max-age=2592000'})

Not sure why this error pops up after a successful upload {'errors': [], 'uploaded': ['dummy.pdf']}

Please Advise


Solution

  • The issue can be solved by,

    Replacing

    remote_name = '/content/sample_data/dummy.pdf'
    

    With

    remote_name = 'dummy.pdf'