I will make a PDF Editor that can change words in the PDF without changing the fromation of the text.
This is my Code so far it does every thing right but i cand get the Text color and the Text size.
import pymupdf
import os
Open the PDF document
doc = pymupdf.open(‘input.pdf’)
Load the custom font file
font_file = os.path.join(os.path.dirname(file), ‘BMWGroupTNCondensed-Regular.otf’)
with open(font_file, ‘rb’) as f:
font_buffer = f.read()
Iterate over each page of the document
for page in doc:
# Find all instances of “2024” on the current page
instances = page.search_for(“2024”)
# Create a list to store the replacement coordinates
replacement_coords = []
# Redact each instance of "2024" on the current page and replace with "2025"
for inst in instances:
page.add_redact_annot(inst)
replacement_coords.append((inst.x0, inst.y0))
# Apply the redactions to the current page
page.apply_redactions()
# Insert the custom font in the PDF document
page.insert_font(fontname="BMWGroupTNCondensed-Regular", fontbuffer=font_buffer)
# Insert the new text "2025" at the redacted locations using the custom font
for x, y in replacement_coords:
page.insert_text((x, y + 10), "2025", fontname="BMWGroupTNCondensed-Regular", fontsize=12)
Save the modified document
doc.save(‘modified_document.pdf’)
Close the document
doc.close()
I tried it with a lot of diverent Bibiliotheks but it didnt work
georg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.