import os
import zipfile
def extract_thumbnail(cdr_path):
try:
with zipfile.ZipFile(cdr_path, 'r') as cdr_file:
thumbnail_path = 'previews/thumbnail.png'
if thumbnail_path in cdr_file.namelist():
parent_name = os.path.splitext(os.path.basename(cdr_path))[0]
output_path = os.path.join(os.path.dirname(cdr_path), f'{parent_name}.png')
with open(output_path, 'wb') as thumbnail_file:
thumbnail_file.write(cdr_file.read(thumbnail_path))
print(f"Thumbnail extracted: {output_path}")
else:
print(f"No thumbnail found in {cdr_path}")
except zipfile.BadZipFile:
print(f"Failed to open {cdr_path}: not a valid zip archive")
def process_directory(directory):
for root, _, files in os.walk(directory):
for file in files:
if file.lower().endswith('.cdr'):
cdr_path = os.path.join(root, file)
extract_thumbnail(cdr_path)
if __name__ == '__main__':
directory = input("Enter the directory to process: ")
process_directory(directory)