Created
May 21, 2025 06:29
-
-
Save florianwns/e68d87ca935dcc0eb99ca0a4c47b4cfc to your computer and use it in GitHub Desktop.
Python script to remove watermark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from pypdf import PdfWriter | |
from pypdf.generic._data_structures import StreamObject | |
books_directory = Path("books") | |
export_directory = Path("export") | |
for input_filepath in books_directory.glob("*.pdf"): | |
output_filepath = export_directory / input_filepath.name | |
print("=====", output_filepath, "=====") | |
writer = PdfWriter(clone_from=str(input_filepath)) | |
for page in writer.pages: | |
contents = page._get_contents_as_bytes() | |
watermark = contents.find(b'/Watermark') | |
if watermark > -1: | |
contents = contents[:watermark - 1] | |
stream = StreamObject() | |
stream.set_data(contents) | |
page.replace_contents(stream) | |
with open(output_filepath, "wb") as f: | |
writer.write(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment