First of all, I tried these questions and didn't work for me:
- Python search and replace in binary file
- python re module to replace the binary data inside a text file?
I'm working in manipulating a pdf file in binary. I need to replace a string by other.
This is my approach:
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
with open("proof.pdf", "rb") as input_file:
content = input_file.read()
if b"21.8182 686.182 261.818 770.182" in content:
print("FOUND!!")
content.replace(b"21.8182 686.182 261.818 770.182", b"1.1 1.1 1.1 1.1")
if b"1.1 1.1 1.1 1.1" in content:
print("REPLACED!!")
with open("proof_output.pdf", "wb") as output_file:
output_file.write(content)
When I run the script it shows "FOUND!!", but not "REPLACED!!"