import io import os import re import shutil import tempfile import zipfile from collections import defaultdict from pathlib import Path import pandas as pd import streamlit as st from PIL import Image as PILImage from reportlab.lib import colors from reportlab.lib.pagesizes import A4 from reportlab.lib.styles import getSampleStyleSheet from reportlab.platypus import ( SimpleDocTemplate, Table, TableStyle, Image as RLImage, Paragraph, Spacer, PageBreak, KeepInFrame ) import openpyxl ALLOWED_IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.tif', '.webp', '.svg', '.ico', '.heic', '.heif', '.raw'} ALLOWED_TEXT_EXTENSIONS = {'.txt', '.md', '.csv', '.doc', '.docx', '.pdf', '.rtf', '.odt', '.tex'} ALLOWED_VIDEO_EXTENSIONS = {'.mp4', '.avi', '.mov', '.wmv', '.flv', '.mkv', '.webm', '.m4v', '.3gp', '.ogv', '.mpg', '.mpeg', '.vob', '.mts', '.m2ts'} ALLOWED_AUDIO_EXTENSIONS = {'.mp3', '.wav', '.aac', '.flac', '.ogg', '.m4a', '.wma', '.opus', '.aiff', '.ape', '.alac'} ALLOWED_ARCHIVE_EXTENSIONS = {'.zip', '.rar', '.7z', '.tar', '.gz', '.bz2', '.xz'} ALLOWED_CODE_EXTENSIONS = {'.py', '.js', '.html', '.css', '.java', '.cpp', '.c', '.h', '.php', '.rb', '.go', '.rs', '.swift', '.kt'} ALLOWED_DATA_EXTENSIONS = {'.json', '.xml', '.yaml', '.yml', '.sql', '.db', '.sqlite', '.xlsx', '.xls'} ALLOWED_OTHER_EXTENSIONS = {'.psd', '.ai', '.sketch', '.fig', '.eps', '.indd', '.dwg', '.stl', '.obj', '.fbx', '.blend'} # Non-image extensions (for highlighting) NON_IMAGE_EXTENSIONS = (ALLOWED_VIDEO_EXTENSIONS | ALLOWED_AUDIO_EXTENSIONS | ALLOWED_TEXT_EXTENSIONS | ALLOWED_ARCHIVE_EXTENSIONS | ALLOWED_CODE_EXTENSIONS | ALLOWED_DATA_EXTENSIONS | ALLOWED_OTHER_EXTENSIONS) # Combine all extensions - if a file has ANY extension or NO extension, process it ALL_ALLOWED_EXTENSIONS = (ALLOWED_IMAGE_EXTENSIONS | ALLOWED_TEXT_EXTENSIONS | ALLOWED_VIDEO_EXTENSIONS | ALLOWED_AUDIO_EXTENSIONS | ALLOWED_ARCHIVE_EXTENSIONS | ALLOWED_CODE_EXTENSIONS | ALLOWED_DATA_EXTENSIONS | ALLOWED_OTHER_EXTENSIONS) # Helper functions from both files def parse_block(folder_name): """Extract the block number and optional lowercase block letter from a folder name. Block folders are briefed as ```` (e.g. ``05Colours``) or, when a block is split into sub-blocks, ```` (e.g. ``05cColours``). The optional block letter is a SINGLE lowercase letter directly after the leading digits. Because category names start with an uppercase letter, a lowercase letter there is unambiguous — ``05Colours`` has no block letter, while ``05cColours`` has block letter ``c``. Returns ``(blocknummer, blockletter)``, e.g. ``("05", "c")`` or ``("05", "")``. """ # Block number: first two digits found, preserving the previous behaviour. blocknummer = re.sub(r'\D', '', folder_name)[:2].zfill(2) # Block letter: a single lowercase letter immediately following the leading digits. match = re.match(r'\d+([a-z])', folder_name) blockletter = match.group(1) if match else '' return blocknummer, blockletter def extract_zip_to_temp(uploaded_file): """Extract uploaded zip file to temporary directory""" temp_dir = tempfile.mkdtemp() with zipfile.ZipFile(uploaded_file, 'r') as zip_ref: zip_ref.extractall(temp_dir) return temp_dir def extract_images_from_xlsx_zip_method(xlsx_path, output_dir, target_sheet=None): """ Fallback method: Extract all images from xlsx using zip method """ try: extracted_files = [] image_count = 0 with zipfile.ZipFile(xlsx_path, 'r') as zip_ref: # Get available sheets available_sheets = [] try: with zip_ref.open('xl/workbook.xml') as f: content = f.read().decode('utf-8') sheet_matches = re.findall(r'