42 lines
1.9 KiB
Python
42 lines
1.9 KiB
Python
import os
|
|
import zipfile
|
|
|
|
SRC = '/home/maxi/Projekt/wtc.waiswuaschd.de/data/import_2026'
|
|
IMG_OUT = '/home/maxi/Projekt/wtc.waiswuaschd.de/app/static/uploads/recipes'
|
|
os.makedirs(IMG_OUT, exist_ok=True)
|
|
|
|
extracted_images = {}
|
|
|
|
for f in sorted(os.listdir(SRC)):
|
|
path = os.path.join(SRC, f)
|
|
base = os.path.splitext(f)[0]
|
|
ext = os.path.splitext(f)[1].lower()
|
|
|
|
if ext in ['.docx', '.pptx']:
|
|
try:
|
|
with zipfile.ZipFile(path, 'r') as z:
|
|
# Find images in word/media or ppt/media
|
|
media_files = [m for m in z.namelist() if m.startswith(('word/media/', 'ppt/media/')) and m.lower().endswith(('.jpg', '.jpeg', '.png'))]
|
|
# Filter out tiny icons (e.g. < 5KB)
|
|
large_media = []
|
|
for m in media_files:
|
|
info = z.getinfo(m)
|
|
if info.file_size > 15000: # > 15KB
|
|
large_media.append((m, info.file_size))
|
|
|
|
large_media.sort(key=lambda x: x[1], reverse=True)
|
|
if large_media:
|
|
best_img = large_media[0][0]
|
|
img_ext = os.path.splitext(best_img)[1].lower()
|
|
safe_name = "".join(c for c in base if c.isalnum() or c in (' ', '_', '-')).strip().replace(' ', '_').lower()
|
|
target_file = f"{safe_name}{img_ext}"
|
|
target_path = os.path.join(IMG_OUT, target_file)
|
|
with open(target_path, 'wb') as out_f:
|
|
out_f.write(z.read(best_img))
|
|
extracted_images[f] = f"/static/uploads/recipes/{target_file}"
|
|
print(f"Extracted image for {f}: {target_file} ({large_media[0][1]} bytes)")
|
|
except Exception as e:
|
|
print(f"Error extracting image from {f}: {e}")
|
|
|
|
print(f"\nTotal images extracted: {len(extracted_images)}")
|