Changing shape color of an existing PDF #4002
-
Hello, First of all, I'm quite new in python, so my question is probably easy to answer, but I've tried somethings and didn't figure out how to solve my self. I generate many PDF files with more than 300 pages. I already use a python code to reorder the pages and I want to recolor all the shapes of one certain color. I know I can do this with Illustrator, but the code will help me a lot. Below is the code I'm using. Using debug I can see that "fill" dict is updated everytime, but once I open the new document the color of the shape is not the new one. I tried shape.update(), shape.commit() and other things.
Thanks and sorry for my english. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Hey @sumanmajumder123, import pymupdf
def alterar_cor(input_pdf, output_pdf, cor_original, nova_cor):
doc = pymupdf.open(input_pdf)
for page in doc:
for shape in page.get_drawings():
if "fill" in shape and shape["fill"] == cor_original:
shape["fill"] = nova_cor
shape.update()
page.clean() # Force redraw of the page
doc.save(output_pdf)
doc.close()
cor_original = (0.13669031858444214, 0.12195010483264923, 0.1252918243408203)
nova_cor = (0.5, 0, 0)
input_pdf = "C:/Users/Sum4n/Desktop/SMP/Gerar arquivos/Entrada.pdf"
output_pdf = "C:/Users/Sum4n/Desktop/SMP/Gerar arquivos/modificado.pdf"
alterar_cor(input_pdf, output_pdf, cor_original, nova_cor) |
Beta Was this translation helpful? Give feedback.
-
page.clean() returned this error: AttributeError: 'Page' object has no attribute 'clean' |
Beta Was this translation helpful? Give feedback.
-
There is no way of changing colors of existing objects on a PDF page! |
Beta Was this translation helpful? Give feedback.
-
Ok. Thanks for the reply! Go back to Illustrator 😭 |
Beta Was this translation helpful? Give feedback.
There is no way of changing colors of existing objects on a PDF page!