Gpt to change text within pdf file

hi

i have a pdf document tht i have uploaded in chat gpt 4. i am asking the text to be changed from a to b within the pdf document. its constantly failing.

can chatgpt perform this action? is there any pdf gpt available? i tried several that are on gpt store but all of them failed and asking me to sign up for paid adobe

please advise

With a small PDF file I was able to successfully convert the letter a to b as requested.

As I have hundreds of PDFs I selected a very small file that has text including the letter a. The file is named yaddatest.pdf and looks like this

image


Using ChatGPT Plus with GPT-4 (Jan 10, 2024)

Upload yaddatest.pdf

You
In the PDF change the letter a to the letter b.

ChatGPT
It appears that my initial approach to modify the text within your PDF encountered a technical issue. I will try a different method to replace all instances of the letter ‘a’ with the letter ‘b’. Let’s proceed with this alternative approach.

The second attempt to modify the PDF also encountered a technical limitation. Given these challenges, I will try a different strategy that involves creating an entirely new PDF document with the modified text. This approach will replicate each page of your original document with ‘a’ replaced by ‘b’. Let’s proceed with this method.

The PDF has been successfully modified with all instances of the letter ‘a’ replaced by the letter ‘b’. You can download the modified file using the link below:

Download the modified PDF


Contents of modified PDF (modified_yaddatest.pdf)

image


First approach that failed. i.e. image

from PyPDF2 import PdfReader, PdfWriter

# File path
file_path = '/mnt/data/yaddatest.pdf'
output_path = '/mnt/data/modified_yaddatest.pdf'

# Create a PDF reader
reader = PdfReader(file_path)
writer = PdfWriter()

# Loop through each page in the PDF
for page in reader.pages:
    # Extract text from the page
    text = page.extract_text()
    
    if text:
        # Replace 'a' with 'b'
        modified_text = text.replace('a', 'b')
        
        # Add modified text to a new page in the writer
        writer.add_page(page)
        writer.update_page_text(page_number=writer.get_page_number(page), 
                                text=modified_text, 
                                flags=4)  # Flags=4 to uncompress text

# Write the modified PDF to a new file
with open(output_path, 'wb') as output_file:
    writer.write(output_file)

output_path

Second method that succeeded, i.e. image

from PyPDF2 import PdfReader, PdfWriter
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter

# Read the original PDF
reader = PdfReader(file_path)
writer = PdfWriter()

# Process each page
for page_num, page in enumerate(reader.pages):
    # Create a PDF with Reportlab to overlay text
    packet = io.BytesIO()
    can = canvas.Canvas(packet, pagesize=letter)
    
    # Extract text from the original PDF page
    text = page.extract_text() or ""
    
    # Replace 'a' with 'b' in the text
    modified_text = text.replace('a', 'b')

    # Draw the modified text onto the canvas
    can.drawString(10, 800, modified_text)  # Adjust position as needed
    can.save()

    # Move to the beginning of the StringIO buffer
    packet.seek(0)
    new_pdf = PdfReader(packet)
    
    # Add the original page to the writer
    writer.add_page(page)

    # Merge the new PDF (with modified text) onto the original page
    writer.add_page(new_pdf.pages[0], page_number=page_num)

# Write the modified PDF to a new file
modified_file_path = '/mnt/data/modified_yaddatest.pdf'
with open(modified_file_path, "wb") as output_pdf:
    writer.write(output_pdf)

modified_file_path