Spaces:
Sleeping
Sleeping
Gnanashri Kota Venkatesh commited on
Enhance file type support and brand extraction
Browse filesUpdated allowed file extensions to include more formats and improved brand extraction logic.
helper.py
CHANGED
|
@@ -18,12 +18,21 @@ from reportlab.platypus import (
|
|
| 18 |
)
|
| 19 |
import openpyxl
|
| 20 |
|
| 21 |
-
|
| 22 |
-
ALLOWED_IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp'}
|
| 23 |
-
ALLOWED_TEXT_EXTENSIONS = {'.txt', '.md', '.csv'}
|
| 24 |
-
ALLOWED_VIDEO_EXTENSIONS = {'.mp4', '.avi', '.mov', '.wmv', '.flv', '.mkv', '.webm', '.m4v', '.3gp', '.ogv'}
|
| 25 |
-
ALLOWED_AUDIO_EXTENSIONS = {'.mp3', '.wav', '.aac', '.flac', '.ogg', '.m4a', '.wma'}
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Helper functions from both files
|
| 29 |
def extract_zip_to_temp(uploaded_file):
|
|
@@ -106,12 +115,22 @@ def process_files(input_folder, marken_index, output_root):
|
|
| 106 |
|
| 107 |
files_to_process = []
|
| 108 |
for file in sorted(subfolder.iterdir()):
|
| 109 |
-
# Skip
|
| 110 |
-
if
|
| 111 |
continue
|
|
|
|
|
|
|
| 112 |
if file.suffix.lower() not in ALL_ALLOWED_EXTENSIONS and file.suffix != '':
|
| 113 |
continue
|
|
|
|
|
|
|
| 114 |
marke = extract_brand(file.stem)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
brand_counters[marke] += 1
|
| 116 |
files_to_process.append((file, marke))
|
| 117 |
|
|
@@ -121,7 +140,12 @@ def process_files(input_folder, marken_index, output_root):
|
|
| 121 |
brand_counters[marke] += 1 # Increment counter for this brand
|
| 122 |
count_str = f"{brand_counters[marke]:02d}" # Format as 01, 02, 03, etc.
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
cleaned = get_cleaned_filename_without_brand(file.name, marke)
|
| 126 |
|
| 127 |
neuer_name = f"{markennummer}B{blocknummer}{marke}{count_str}{cleaned}{file.suffix.lower()}"
|
|
@@ -135,7 +159,7 @@ def process_files(input_folder, marken_index, output_root):
|
|
| 135 |
else:
|
| 136 |
img.convert("RGBA").save(ziel, format='PNG')
|
| 137 |
except Exception as e:
|
| 138 |
-
|
| 139 |
shutil.copy2(file, ziel)
|
| 140 |
else:
|
| 141 |
shutil.copy2(file, ziel)
|
|
@@ -145,57 +169,127 @@ def process_files(input_folder, marken_index, output_root):
|
|
| 145 |
|
| 146 |
return renamed_files_by_folder_and_marke, file_to_factorgroup
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
def generate_excel_report(output_folder: Path, marken_index: dict, file_to_factorgroup: dict):
|
|
|
|
| 149 |
final_excel_path = output_folder / "IcAt_Overview_Final.xlsx"
|
| 150 |
|
| 151 |
nummer_zu_marke = {v: k for k, v in marken_index.items()}
|
| 152 |
data = []
|
|
|
|
| 153 |
for file in sorted(output_folder.iterdir()):
|
| 154 |
-
if file.is_file() and
|
| 155 |
name = file.stem
|
|
|
|
|
|
|
| 156 |
match = re.match(r"(\d{2})B(\d{2})([a-z0-9]+)", name, re.IGNORECASE)
|
| 157 |
if match:
|
| 158 |
markennummer, blocknummer, marke = match.groups()
|
| 159 |
factor = nummer_zu_marke.get(markennummer, marke)
|
| 160 |
-
|
| 161 |
-
# Get factorgroup and remove underscores
|
| 162 |
factorgroup = file_to_factorgroup.get(file.name, f"{blocknummer}Unknown")
|
| 163 |
factorgroup = factorgroup.replace('_', '') # Remove all underscores
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
data.append({
|
| 166 |
"factor": factor,
|
| 167 |
"factorgroup": factorgroup,
|
| 168 |
-
"ID":
|
|
|
|
|
|
|
| 169 |
})
|
| 170 |
|
| 171 |
-
|
|
|
|
| 172 |
|
| 173 |
reordered_data = []
|
| 174 |
for _, row in df_assets.iterrows():
|
| 175 |
-
|
| 176 |
-
group_prefix =
|
| 177 |
try:
|
| 178 |
group = str(int(group_prefix))
|
| 179 |
except ValueError:
|
| 180 |
group = group_prefix
|
| 181 |
|
| 182 |
-
# Also remove underscores from the reordered data
|
| 183 |
clean_factor = str(row["factorgroup"]).replace('_', '')
|
| 184 |
clean_factorgroup = str(row["factor"]).replace('_', '')
|
| 185 |
-
|
| 186 |
reordered_data.append({
|
| 187 |
"Group": group,
|
| 188 |
"factor": clean_factor,
|
| 189 |
"factorgroup": clean_factorgroup,
|
| 190 |
-
"ID": row["ID"]
|
|
|
|
| 191 |
})
|
| 192 |
|
| 193 |
-
df_reordered = pd.DataFrame(reordered_data, columns=["Group", "factor", "factorgroup", "ID"])
|
| 194 |
-
df_reordered = df_reordered
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
with pd.ExcelWriter(final_excel_path, engine='openpyxl') as writer:
|
| 196 |
df_assets.to_excel(writer, index=False, sheet_name="Assets")
|
| 197 |
df_reordered.to_excel(writer, index=False, sheet_name="Reordered")
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
return final_excel_path
|
| 200 |
|
| 201 |
|
|
@@ -492,9 +586,32 @@ def extract_images_from_xlsx(xlsx_file, output_dir):
|
|
| 492 |
return 0, [], str(e)
|
| 493 |
|
| 494 |
def extract_brand(file_stem):
|
| 495 |
-
"""Extract brand name from filename using pattern matching"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
parts = re.split(r'[ _\-]', file_stem)
|
| 497 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 498 |
|
| 499 |
def erkenne_marken_aus_ordnern(input_folder):
|
| 500 |
"""Recognize brands from subfolders (original method)"""
|
|
@@ -713,7 +830,7 @@ def add_brand_pages(elements, marken_spalten, renamed_files_by_folder_and_marke)
|
|
| 713 |
elements.append(table)
|
| 714 |
|
| 715 |
def analyze_files_by_filename(input_folder):
|
| 716 |
-
"""Analyze files by extracting brand names from filenames"""
|
| 717 |
dateien = []
|
| 718 |
marken_set = set()
|
| 719 |
renamed_files_by_folder_and_marke = defaultdict(lambda: defaultdict(list))
|
|
@@ -723,9 +840,20 @@ def analyze_files_by_filename(input_folder):
|
|
| 723 |
continue
|
| 724 |
|
| 725 |
for file in sorted(subfolder.iterdir()):
|
| 726 |
-
if file.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 727 |
continue
|
|
|
|
| 728 |
marke = extract_brand(file.stem)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
marken_set.add(marke)
|
| 730 |
dateien.append({
|
| 731 |
"original_file": file,
|
|
@@ -790,17 +918,17 @@ def extract_zip_to_temp(uploaded_file):
|
|
| 790 |
zip_ref.extractall(temp_dir)
|
| 791 |
return temp_dir
|
| 792 |
|
| 793 |
-
def extract_brand(file_stem):
|
| 794 |
-
"""Extract brand name from filename using pattern matching"""
|
| 795 |
-
parts = re.split(r'[ _\-]', file_stem)
|
| 796 |
-
return re.sub(r'[^a-z0-9]', '', parts[0].lower()) if parts else "unknown"
|
| 797 |
-
|
| 798 |
def clean_letters_only(text):
|
| 799 |
return re.sub(r'[^A-Za-z]', '', text)
|
| 800 |
|
| 801 |
def get_cleaned_filename_without_brand(filename, brand):
|
| 802 |
-
|
| 803 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 804 |
|
| 805 |
def generate_filename_based_pdf_report(input_folder, erste_marke=None, processed_files_mapping=None):
|
| 806 |
"""Generate PDF report based on filename brand analysis with final processed labels"""
|
|
|
|
| 18 |
)
|
| 19 |
import openpyxl
|
| 20 |
|
| 21 |
+
|
| 22 |
+
ALLOWED_IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.tif', '.webp', '.svg', '.ico', '.heic', '.heif', '.raw'}
|
| 23 |
+
ALLOWED_TEXT_EXTENSIONS = {'.txt', '.md', '.csv', '.doc', '.docx', '.pdf', '.rtf', '.odt', '.tex'}
|
| 24 |
+
ALLOWED_VIDEO_EXTENSIONS = {'.mp4', '.avi', '.mov', '.wmv', '.flv', '.mkv', '.webm', '.m4v', '.3gp', '.ogv', '.mpg', '.mpeg', '.vob', '.mts', '.m2ts'}
|
| 25 |
+
ALLOWED_AUDIO_EXTENSIONS = {'.mp3', '.wav', '.aac', '.flac', '.ogg', '.m4a', '.wma', '.opus', '.aiff', '.ape', '.alac'}
|
| 26 |
+
ALLOWED_ARCHIVE_EXTENSIONS = {'.zip', '.rar', '.7z', '.tar', '.gz', '.bz2', '.xz'}
|
| 27 |
+
ALLOWED_CODE_EXTENSIONS = {'.py', '.js', '.html', '.css', '.java', '.cpp', '.c', '.h', '.php', '.rb', '.go', '.rs', '.swift', '.kt'}
|
| 28 |
+
ALLOWED_DATA_EXTENSIONS = {'.json', '.xml', '.yaml', '.yml', '.sql', '.db', '.sqlite', '.xlsx', '.xls'}
|
| 29 |
+
ALLOWED_OTHER_EXTENSIONS = {'.psd', '.ai', '.sketch', '.fig', '.eps', '.indd', '.dwg', '.stl', '.obj', '.fbx', '.blend'}
|
| 30 |
+
|
| 31 |
+
# Combine all extensions - if a file has ANY extension or NO extension, process it
|
| 32 |
+
ALL_ALLOWED_EXTENSIONS = (ALLOWED_IMAGE_EXTENSIONS | ALLOWED_TEXT_EXTENSIONS |
|
| 33 |
+
ALLOWED_VIDEO_EXTENSIONS | ALLOWED_AUDIO_EXTENSIONS |
|
| 34 |
+
ALLOWED_ARCHIVE_EXTENSIONS | ALLOWED_CODE_EXTENSIONS |
|
| 35 |
+
ALLOWED_DATA_EXTENSIONS | ALLOWED_OTHER_EXTENSIONS)
|
| 36 |
|
| 37 |
# Helper functions from both files
|
| 38 |
def extract_zip_to_temp(uploaded_file):
|
|
|
|
| 115 |
|
| 116 |
files_to_process = []
|
| 117 |
for file in sorted(subfolder.iterdir()):
|
| 118 |
+
# Skip if not a file
|
| 119 |
+
if not file.is_file():
|
| 120 |
continue
|
| 121 |
+
|
| 122 |
+
# Skip files without allowed extensions
|
| 123 |
if file.suffix.lower() not in ALL_ALLOWED_EXTENSIONS and file.suffix != '':
|
| 124 |
continue
|
| 125 |
+
|
| 126 |
+
# Accept files with no extension as well
|
| 127 |
marke = extract_brand(file.stem)
|
| 128 |
+
|
| 129 |
+
# CRITICAL FIX: Skip files where brand extraction failed or brand not in index
|
| 130 |
+
if not marke or marke == "unknown" or marke not in marken_index:
|
| 131 |
+
print(f"Warning: Skipping file {file.name} - brand '{marke}' not found in marken_index")
|
| 132 |
+
continue
|
| 133 |
+
|
| 134 |
brand_counters[marke] += 1
|
| 135 |
files_to_process.append((file, marke))
|
| 136 |
|
|
|
|
| 140 |
brand_counters[marke] += 1 # Increment counter for this brand
|
| 141 |
count_str = f"{brand_counters[marke]:02d}" # Format as 01, 02, 03, etc.
|
| 142 |
|
| 143 |
+
# CRITICAL FIX: Double-check brand exists in index before proceeding
|
| 144 |
+
if marke not in marken_index:
|
| 145 |
+
print(f"Warning: Skipping file {file.name} - brand '{marke}' not in marken_index")
|
| 146 |
+
continue
|
| 147 |
+
|
| 148 |
+
markennummer = marken_index[marke]
|
| 149 |
cleaned = get_cleaned_filename_without_brand(file.name, marke)
|
| 150 |
|
| 151 |
neuer_name = f"{markennummer}B{blocknummer}{marke}{count_str}{cleaned}{file.suffix.lower()}"
|
|
|
|
| 159 |
else:
|
| 160 |
img.convert("RGBA").save(ziel, format='PNG')
|
| 161 |
except Exception as e:
|
| 162 |
+
print(f"Error processing image {file.name}: {e}")
|
| 163 |
shutil.copy2(file, ziel)
|
| 164 |
else:
|
| 165 |
shutil.copy2(file, ziel)
|
|
|
|
| 169 |
|
| 170 |
return renamed_files_by_folder_and_marke, file_to_factorgroup
|
| 171 |
|
| 172 |
+
def is_valid_file(file_path):
|
| 173 |
+
"""Check if file should be processed - accept ALL files except hidden/system files"""
|
| 174 |
+
filename = Path(file_path).name
|
| 175 |
+
if filename.startswith('.') or filename.startswith('~') or filename == 'Thumbs.db':
|
| 176 |
+
return False
|
| 177 |
+
return True
|
| 178 |
+
|
| 179 |
def generate_excel_report(output_folder: Path, marken_index: dict, file_to_factorgroup: dict):
|
| 180 |
+
"""Generate Excel report - ACCEPT ALL FILE TYPES"""
|
| 181 |
final_excel_path = output_folder / "IcAt_Overview_Final.xlsx"
|
| 182 |
|
| 183 |
nummer_zu_marke = {v: k for k, v in marken_index.items()}
|
| 184 |
data = []
|
| 185 |
+
|
| 186 |
for file in sorted(output_folder.iterdir()):
|
| 187 |
+
if file.is_file() and is_valid_file(file):
|
| 188 |
name = file.stem
|
| 189 |
+
file_ext = file.suffix.lower() if file.suffix else ''
|
| 190 |
+
|
| 191 |
match = re.match(r"(\d{2})B(\d{2})([a-z0-9]+)", name, re.IGNORECASE)
|
| 192 |
if match:
|
| 193 |
markennummer, blocknummer, marke = match.groups()
|
| 194 |
factor = nummer_zu_marke.get(markennummer, marke)
|
| 195 |
+
|
|
|
|
| 196 |
factorgroup = file_to_factorgroup.get(file.name, f"{blocknummer}Unknown")
|
| 197 |
factorgroup = factorgroup.replace('_', '') # Remove all underscores
|
| 198 |
+
|
| 199 |
+
only_id = name
|
| 200 |
+
is_text_file = False
|
| 201 |
+
is_b20_id = False
|
| 202 |
+
|
| 203 |
+
if "B20" in only_id.upper():
|
| 204 |
+
is_b20_id = True
|
| 205 |
+
language_value = only_id + ".png"
|
| 206 |
+
elif file_ext == '.txt':
|
| 207 |
+
is_text_file = True
|
| 208 |
+
try:
|
| 209 |
+
with open(file, 'r', encoding='utf-8', errors='ignore') as txt_file:
|
| 210 |
+
txt_content = txt_file.read().strip()
|
| 211 |
+
language_value = txt_content if txt_content else "[Empty file]"
|
| 212 |
+
except Exception as e:
|
| 213 |
+
language_value = f"[Error reading file: {str(e)}]"
|
| 214 |
+
else:
|
| 215 |
+
language_value = name + file_ext
|
| 216 |
+
|
| 217 |
data.append({
|
| 218 |
"factor": factor,
|
| 219 |
"factorgroup": factorgroup,
|
| 220 |
+
"ID": only_id,
|
| 221 |
+
"Language": language_value,
|
| 222 |
+
"highlight_yellow": is_text_file or is_b20_id
|
| 223 |
})
|
| 224 |
|
| 225 |
+
|
| 226 |
+
df_assets = pd.DataFrame(data, columns=["factor", "factorgroup", "ID", "Language"])
|
| 227 |
|
| 228 |
reordered_data = []
|
| 229 |
for _, row in df_assets.iterrows():
|
| 230 |
+
raw_factor = str(row["factorgroup"])
|
| 231 |
+
group_prefix = raw_factor[:2]
|
| 232 |
try:
|
| 233 |
group = str(int(group_prefix))
|
| 234 |
except ValueError:
|
| 235 |
group = group_prefix
|
| 236 |
|
|
|
|
| 237 |
clean_factor = str(row["factorgroup"]).replace('_', '')
|
| 238 |
clean_factorgroup = str(row["factor"]).replace('_', '')
|
| 239 |
+
|
| 240 |
reordered_data.append({
|
| 241 |
"Group": group,
|
| 242 |
"factor": clean_factor,
|
| 243 |
"factorgroup": clean_factorgroup,
|
| 244 |
+
"ID": row["ID"],
|
| 245 |
+
"Language": row["Language"]
|
| 246 |
})
|
| 247 |
|
| 248 |
+
df_reordered = pd.DataFrame(reordered_data, columns=["Group", "factor", "factorgroup", "ID", "Language"])
|
| 249 |
+
df_reordered['factor'] = df_reordered['factor'].astype(str)
|
| 250 |
+
|
| 251 |
+
df_reordered['Group_num'] = pd.to_numeric(df_reordered['Group'], errors='coerce').fillna(999).astype(int)
|
| 252 |
+
|
| 253 |
+
df_reordered['factor_num'] = df_reordered['factor'].str.extract(r'^(\d+)')[0]
|
| 254 |
+
df_reordered['factor_num'] = pd.to_numeric(df_reordered['factor_num'], errors='coerce').fillna(999).astype(int)
|
| 255 |
+
|
| 256 |
+
df_reordered = df_reordered.sort_values(
|
| 257 |
+
by=['Group_num', 'factor_num', 'factor'],
|
| 258 |
+
ascending=[True, True, True]
|
| 259 |
+
).reset_index(drop=True)
|
| 260 |
+
|
| 261 |
+
df_reordered = df_reordered.drop(['factor_num', 'Group_num'], axis=1)
|
| 262 |
+
|
| 263 |
with pd.ExcelWriter(final_excel_path, engine='openpyxl') as writer:
|
| 264 |
df_assets.to_excel(writer, index=False, sheet_name="Assets")
|
| 265 |
df_reordered.to_excel(writer, index=False, sheet_name="Reordered")
|
| 266 |
|
| 267 |
+
workbook = writer.book
|
| 268 |
+
from openpyxl.styles import PatternFill
|
| 269 |
+
|
| 270 |
+
yellow_fill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid')
|
| 271 |
+
|
| 272 |
+
assets_sheet = workbook['Assets']
|
| 273 |
+
language_col_idx = 4 # Language is the 4th column (D)
|
| 274 |
+
for row_idx in range(2, len(df_assets) + 2): # Start from row 2 (after header)
|
| 275 |
+
cell = assets_sheet.cell(row=row_idx, column=language_col_idx)
|
| 276 |
+
if row_idx - 2 < len(data):
|
| 277 |
+
if data[row_idx - 2].get("highlight_yellow", False):
|
| 278 |
+
cell.fill = yellow_fill
|
| 279 |
+
|
| 280 |
+
reordered_sheet = workbook['Reordered']
|
| 281 |
+
language_col_idx = 5 # Language is the 5th column (E)
|
| 282 |
+
id_col_idx = 4 # ID is the 4th column (D)
|
| 283 |
+
|
| 284 |
+
id_to_highlight = {item["ID"]: item.get("highlight_yellow", False) for item in data}
|
| 285 |
+
|
| 286 |
+
for row_idx in range(2, len(df_reordered) + 2): # Start from row 2 (after header)
|
| 287 |
+
cell = reordered_sheet.cell(row=row_idx, column=language_col_idx)
|
| 288 |
+
id_cell = reordered_sheet.cell(row=row_idx, column=id_col_idx)
|
| 289 |
+
id_value = id_cell.value
|
| 290 |
+
if id_value and id_to_highlight.get(id_value, False):
|
| 291 |
+
cell.fill = yellow_fill
|
| 292 |
+
|
| 293 |
return final_excel_path
|
| 294 |
|
| 295 |
|
|
|
|
| 586 |
return 0, [], str(e)
|
| 587 |
|
| 588 |
def extract_brand(file_stem):
|
| 589 |
+
"""Extract brand name from filename using pattern matching - IMPROVED VERSION"""
|
| 590 |
+
# Remove any leading/trailing whitespace
|
| 591 |
+
file_stem = file_stem.strip()
|
| 592 |
+
|
| 593 |
+
# Split by common delimiters
|
| 594 |
parts = re.split(r'[ _\-]', file_stem)
|
| 595 |
+
|
| 596 |
+
if not parts:
|
| 597 |
+
return "unknown"
|
| 598 |
+
|
| 599 |
+
# Take the first part and clean it
|
| 600 |
+
first_part = parts[0].lower()
|
| 601 |
+
# Remove all non-alphanumeric characters
|
| 602 |
+
cleaned = re.sub(r'[^a-z0-9]', '', first_part)
|
| 603 |
+
|
| 604 |
+
# If the result is empty or too short, return unknown
|
| 605 |
+
if not cleaned or len(cleaned) < 2:
|
| 606 |
+
return "unknown"
|
| 607 |
+
|
| 608 |
+
# If it starts with numbers (like "01b01"), it might be already processed - extract the brand part
|
| 609 |
+
# Pattern: digits + 'b' + digits + brand
|
| 610 |
+
match = re.match(r'^\d{2}b\d{2}([a-z]+)', cleaned)
|
| 611 |
+
if match:
|
| 612 |
+
return match.group(1)
|
| 613 |
+
|
| 614 |
+
return cleaned
|
| 615 |
|
| 616 |
def erkenne_marken_aus_ordnern(input_folder):
|
| 617 |
"""Recognize brands from subfolders (original method)"""
|
|
|
|
| 830 |
elements.append(table)
|
| 831 |
|
| 832 |
def analyze_files_by_filename(input_folder):
|
| 833 |
+
"""Analyze files by extracting brand names from filenames - IMPROVED VERSION"""
|
| 834 |
dateien = []
|
| 835 |
marken_set = set()
|
| 836 |
renamed_files_by_folder_and_marke = defaultdict(lambda: defaultdict(list))
|
|
|
|
| 840 |
continue
|
| 841 |
|
| 842 |
for file in sorted(subfolder.iterdir()):
|
| 843 |
+
if not file.is_file():
|
| 844 |
+
continue
|
| 845 |
+
|
| 846 |
+
# Accept files with allowed extensions or no extension
|
| 847 |
+
if file.suffix.lower() not in ALL_ALLOWED_EXTENSIONS and file.suffix != '':
|
| 848 |
continue
|
| 849 |
+
|
| 850 |
marke = extract_brand(file.stem)
|
| 851 |
+
|
| 852 |
+
# Skip unknown brands
|
| 853 |
+
if marke == "unknown":
|
| 854 |
+
print(f"Warning: Could not extract brand from filename: {file.name}")
|
| 855 |
+
continue
|
| 856 |
+
|
| 857 |
marken_set.add(marke)
|
| 858 |
dateien.append({
|
| 859 |
"original_file": file,
|
|
|
|
| 918 |
zip_ref.extractall(temp_dir)
|
| 919 |
return temp_dir
|
| 920 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 921 |
def clean_letters_only(text):
|
| 922 |
return re.sub(r'[^A-Za-z]', '', text)
|
| 923 |
|
| 924 |
def get_cleaned_filename_without_brand(filename, brand):
|
| 925 |
+
"""Get cleaned filename without brand name"""
|
| 926 |
+
# Remove the extension first if present
|
| 927 |
+
stem = Path(filename).stem
|
| 928 |
+
full_letters = clean_letters_only(stem.lower())
|
| 929 |
+
# Remove the brand from the beginning
|
| 930 |
+
cleaned = full_letters.replace(brand, '', 1)
|
| 931 |
+
return cleaned
|
| 932 |
|
| 933 |
def generate_filename_based_pdf_report(input_folder, erste_marke=None, processed_files_mapping=None):
|
| 934 |
"""Generate PDF report based on filename brand analysis with final processed labels"""
|