75 lines
1.7 KiB
Python
75 lines
1.7 KiB
Python
import fitz
|
|
from pathlib import Path
|
|
import platform
|
|
import sys
|
|
import tempfile
|
|
import os
|
|
|
|
HOSTNAME = platform.node().upper()
|
|
|
|
if HOSTNAME in {"SESTRA", "POHODA","LENOVO"}:
|
|
DROPBOX_DRIVE = Path(r"Z:\\")
|
|
elif HOSTNAME == "Z230":
|
|
DROPBOX_DRIVE = Path(r"U:\\")
|
|
else:
|
|
print(f"❌ Unknown computer name: {HOSTNAME}")
|
|
sys.exit(1)
|
|
|
|
BASE_DIR = (
|
|
DROPBOX_DRIVE
|
|
/ "Dropbox"
|
|
/ "Ordinace"
|
|
/ "Dokumentace_ke_zpracování"
|
|
/ "AdobeFlattenStamp"
|
|
)
|
|
|
|
def flatten_pdf_rasterize_overwrite(input_pdf: Path):
|
|
print(f"Processing: {input_pdf.name}")
|
|
|
|
doc = fitz.open(input_pdf)
|
|
new_doc = fitz.open()
|
|
|
|
for page in doc:
|
|
pix = page.get_pixmap(dpi=400)
|
|
new_page = new_doc.new_page(
|
|
width=page.rect.width,
|
|
height=page.rect.height
|
|
)
|
|
new_page.insert_image(new_page.rect, pixmap=pix)
|
|
|
|
# safe overwrite
|
|
with tempfile.NamedTemporaryFile(
|
|
suffix=".pdf",
|
|
delete=False,
|
|
dir=input_pdf.parent
|
|
) as tmp:
|
|
tmp_path = Path(tmp.name)
|
|
|
|
new_doc.save(tmp_path, deflate=True)
|
|
new_doc.close()
|
|
doc.close()
|
|
|
|
os.replace(tmp_path, input_pdf)
|
|
print(f" ✔ Overwritten: {input_pdf.name}")
|
|
|
|
def main():
|
|
print(f"🖥 Running on: {HOSTNAME}")
|
|
print(f"📁 Base directory: {BASE_DIR}")
|
|
|
|
if not BASE_DIR.exists():
|
|
print("❌ Base directory does not exist!")
|
|
sys.exit(1)
|
|
|
|
pdfs = list(BASE_DIR.glob("*.pdf"))
|
|
if not pdfs:
|
|
print("No PDF files found.")
|
|
return
|
|
|
|
for pdf in pdfs:
|
|
flatten_pdf_rasterize_overwrite(pdf)
|
|
|
|
print("\n✅ All files processed and originals overwritten.")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|