Spaces:
Sleeping
Sleeping
venkatesh commited on
Commit ·
0dd72c7
1
Parent(s): 4b6d20a
Reordered sheet fix of order
Browse files- __pycache__/helper.cpython-313.pyc +0 -0
- __pycache__/tab8.cpython-313.pyc +0 -0
- helper.py +4 -37
__pycache__/helper.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/helper.cpython-313.pyc and b/__pycache__/helper.cpython-313.pyc differ
|
|
|
__pycache__/tab8.cpython-313.pyc
ADDED
|
Binary file (523 Bytes). View file
|
|
|
helper.py
CHANGED
|
@@ -111,10 +111,8 @@ def process_files(input_folder, marken_index, output_root):
|
|
| 111 |
continue
|
| 112 |
blocknummer = re.sub(r'\D', '', subfolder.name)[:2].zfill(2)
|
| 113 |
|
| 114 |
-
# Count files per brand in this folder for sequential numbering
|
| 115 |
brand_counters = defaultdict(int)
|
| 116 |
|
| 117 |
-
# First pass: count files per brand to establish proper numbering
|
| 118 |
files_to_process = []
|
| 119 |
for file in sorted(subfolder.iterdir()):
|
| 120 |
if file.suffix.lower() not in ALL_ALLOWED_EXTENSIONS:
|
|
@@ -123,10 +121,8 @@ def process_files(input_folder, marken_index, output_root):
|
|
| 123 |
brand_counters[marke] += 1
|
| 124 |
files_to_process.append((file, marke))
|
| 125 |
|
| 126 |
-
# Reset counters for actual processing
|
| 127 |
brand_counters = defaultdict(int)
|
| 128 |
|
| 129 |
-
# Second pass: process files with sequential numbering
|
| 130 |
for file, marke in files_to_process:
|
| 131 |
brand_counters[marke] += 1 # Increment counter for this brand
|
| 132 |
count_str = f"{brand_counters[marke]:02d}" # Format as 01, 02, 03, etc.
|
|
@@ -134,18 +130,14 @@ def process_files(input_folder, marken_index, output_root):
|
|
| 134 |
markennummer = marken_index[marke]
|
| 135 |
cleaned = get_cleaned_filename_without_brand(file.name, marke)
|
| 136 |
|
| 137 |
-
# New format: markennummer + B + blocknummer + marke + count + cleaned + extension
|
| 138 |
neuer_name = f"{markennummer}B{blocknummer}{marke}{count_str}{cleaned}{file.suffix.lower()}"
|
| 139 |
ziel = output_root / neuer_name
|
| 140 |
|
| 141 |
if file.suffix.lower() in ALLOWED_IMAGE_EXTENSIONS:
|
| 142 |
with PILImage.open(file) as img:
|
| 143 |
-
# Preserve transparency instead of converting to RGB
|
| 144 |
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
|
| 145 |
-
# If image already has transparency, preserve it
|
| 146 |
img.save(ziel, format='PNG')
|
| 147 |
else:
|
| 148 |
-
# For images without transparency, convert to RGBA to allow transparency
|
| 149 |
img.convert("RGBA").save(ziel, format='PNG')
|
| 150 |
else:
|
| 151 |
shutil.copy2(file, ziel)
|
|
@@ -158,10 +150,8 @@ def process_files(input_folder, marken_index, output_root):
|
|
| 158 |
def is_valid_file(file_path):
|
| 159 |
"""Check if file should be processed - accept ALL files except hidden/system files"""
|
| 160 |
filename = Path(file_path).name
|
| 161 |
-
# Skip hidden files and system files
|
| 162 |
if filename.startswith('.') or filename.startswith('~') or filename == 'Thumbs.db':
|
| 163 |
return False
|
| 164 |
-
# Accept all other files
|
| 165 |
return True
|
| 166 |
|
| 167 |
def generate_excel_report(output_folder: Path, marken_index: dict, file_to_factorgroup: dict):
|
|
@@ -172,10 +162,8 @@ def generate_excel_report(output_folder: Path, marken_index: dict, file_to_facto
|
|
| 172 |
data = []
|
| 173 |
|
| 174 |
for file in sorted(output_folder.iterdir()):
|
| 175 |
-
# Process ALL files, not just specific extensions
|
| 176 |
if file.is_file() and is_valid_file(file):
|
| 177 |
name = file.stem
|
| 178 |
-
# Get file extension for display
|
| 179 |
file_ext = file.suffix.lower() if file.suffix else ''
|
| 180 |
|
| 181 |
match = re.match(r"(\d{2})B(\d{2})([a-z0-9]+)", name, re.IGNORECASE)
|
|
@@ -183,34 +171,25 @@ def generate_excel_report(output_folder: Path, marken_index: dict, file_to_facto
|
|
| 183 |
markennummer, blocknummer, marke = match.groups()
|
| 184 |
factor = nummer_zu_marke.get(markennummer, marke)
|
| 185 |
|
| 186 |
-
# Get factorgroup and remove underscores
|
| 187 |
factorgroup = file_to_factorgroup.get(file.name, f"{blocknummer}Unknown")
|
| 188 |
factorgroup = factorgroup.replace('_', '') # Remove all underscores
|
| 189 |
|
| 190 |
-
# Include file extension in the ID column
|
| 191 |
only_id = name
|
| 192 |
is_text_file = False
|
| 193 |
is_b20_id = False
|
| 194 |
|
| 195 |
-
# Check if ID contains "B20" substring
|
| 196 |
if "B20" in only_id.upper():
|
| 197 |
is_b20_id = True
|
| 198 |
-
# Rule 3: If ID contains B20, always use .png
|
| 199 |
language_value = only_id + ".png"
|
| 200 |
elif file_ext == '.txt':
|
| 201 |
-
# Rule 2: Read text file content
|
| 202 |
is_text_file = True
|
| 203 |
try:
|
| 204 |
-
# Read the content of the .txt file
|
| 205 |
with open(file, 'r', encoding='utf-8', errors='ignore') as txt_file:
|
| 206 |
txt_content = txt_file.read().strip()
|
| 207 |
-
# Use the content instead of filename
|
| 208 |
language_value = txt_content if txt_content else "[Empty file]"
|
| 209 |
except Exception as e:
|
| 210 |
-
# If error reading file, show error message
|
| 211 |
language_value = f"[Error reading file: {str(e)}]"
|
| 212 |
else:
|
| 213 |
-
# Rule 1: For normal files, show ID + extension
|
| 214 |
language_value = name + file_ext
|
| 215 |
|
| 216 |
data.append({
|
|
@@ -218,7 +197,7 @@ def generate_excel_report(output_folder: Path, marken_index: dict, file_to_facto
|
|
| 218 |
"factorgroup": factorgroup,
|
| 219 |
"ID": only_id,
|
| 220 |
"Language": language_value,
|
| 221 |
-
"highlight_yellow": is_text_file or is_b20_id
|
| 222 |
})
|
| 223 |
|
| 224 |
|
|
@@ -226,16 +205,15 @@ def generate_excel_report(output_folder: Path, marken_index: dict, file_to_facto
|
|
| 226 |
|
| 227 |
reordered_data = []
|
| 228 |
for _, row in df_assets.iterrows():
|
| 229 |
-
|
| 230 |
-
group_prefix =
|
| 231 |
try:
|
| 232 |
group = str(int(group_prefix))
|
| 233 |
except ValueError:
|
| 234 |
group = group_prefix
|
| 235 |
|
| 236 |
-
# Also remove underscores from the reordered data
|
| 237 |
clean_factor = str(row["factorgroup"]).replace('_', '')
|
| 238 |
-
clean_factorgroup = str(row["
|
| 239 |
|
| 240 |
reordered_data.append({
|
| 241 |
"Group": group,
|
|
@@ -248,53 +226,43 @@ def generate_excel_report(output_folder: Path, marken_index: dict, file_to_facto
|
|
| 248 |
df_reordered = pd.DataFrame(reordered_data, columns=["Group", "factor", "factorgroup", "ID", "Language"])
|
| 249 |
df_reordered['factor'] = df_reordered['factor'].astype(str)
|
| 250 |
|
| 251 |
-
# Convert Group to numeric for proper sorting
|
| 252 |
df_reordered['Group_num'] = pd.to_numeric(df_reordered['Group'], errors='coerce').fillna(999).astype(int)
|
| 253 |
|
| 254 |
-
# Extract numeric prefix from factor for proper sorting
|
| 255 |
df_reordered['factor_num'] = df_reordered['factor'].str.extract(r'^(\d+)')[0]
|
| 256 |
df_reordered['factor_num'] = pd.to_numeric(df_reordered['factor_num'], errors='coerce').fillna(999).astype(int)
|
| 257 |
|
| 258 |
-
# Sort by Group (numeric), then by factor numeric prefix, then by factor string
|
| 259 |
df_reordered = df_reordered.sort_values(
|
| 260 |
by=['Group_num', 'factor_num', 'factor'],
|
| 261 |
ascending=[True, True, True]
|
| 262 |
).reset_index(drop=True)
|
| 263 |
|
| 264 |
-
# Remove the temporary sorting columns
|
| 265 |
df_reordered = df_reordered.drop(['factor_num', 'Group_num'], axis=1)
|
| 266 |
|
| 267 |
with pd.ExcelWriter(final_excel_path, engine='openpyxl') as writer:
|
| 268 |
df_assets.to_excel(writer, index=False, sheet_name="Assets")
|
| 269 |
df_reordered.to_excel(writer, index=False, sheet_name="Reordered")
|
| 270 |
|
| 271 |
-
# Apply yellow highlighting to Language column cells
|
| 272 |
workbook = writer.book
|
| 273 |
from openpyxl.styles import PatternFill
|
| 274 |
|
| 275 |
yellow_fill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid')
|
| 276 |
|
| 277 |
-
# Highlight in Assets sheet
|
| 278 |
assets_sheet = workbook['Assets']
|
| 279 |
language_col_idx = 4 # Language is the 4th column (D)
|
| 280 |
for row_idx in range(2, len(df_assets) + 2): # Start from row 2 (after header)
|
| 281 |
cell = assets_sheet.cell(row=row_idx, column=language_col_idx)
|
| 282 |
-
# Check if this row should be highlighted based on the highlight_yellow flag
|
| 283 |
if row_idx - 2 < len(data):
|
| 284 |
if data[row_idx - 2].get("highlight_yellow", False):
|
| 285 |
cell.fill = yellow_fill
|
| 286 |
|
| 287 |
-
# Highlight in Reordered sheet
|
| 288 |
reordered_sheet = workbook['Reordered']
|
| 289 |
language_col_idx = 5 # Language is the 5th column (E)
|
| 290 |
id_col_idx = 4 # ID is the 4th column (D)
|
| 291 |
|
| 292 |
-
# Create a mapping of ID to highlight_yellow flag for quick lookup
|
| 293 |
id_to_highlight = {item["ID"]: item.get("highlight_yellow", False) for item in data}
|
| 294 |
|
| 295 |
for row_idx in range(2, len(df_reordered) + 2): # Start from row 2 (after header)
|
| 296 |
cell = reordered_sheet.cell(row=row_idx, column=language_col_idx)
|
| 297 |
-
# Read the ID from this row to check if it should be highlighted
|
| 298 |
id_cell = reordered_sheet.cell(row=row_idx, column=id_col_idx)
|
| 299 |
id_value = id_cell.value
|
| 300 |
if id_value and id_to_highlight.get(id_value, False):
|
|
@@ -303,7 +271,6 @@ def generate_excel_report(output_folder: Path, marken_index: dict, file_to_facto
|
|
| 303 |
return final_excel_path
|
| 304 |
|
| 305 |
|
| 306 |
-
|
| 307 |
def add_brand_pages_with_png_extensions(elements, marken_spalten, renamed_files_by_folder_and_marke, marken_index):
|
| 308 |
"""Add brand overview pages with .png extensions in labels"""
|
| 309 |
styles = getSampleStyleSheet()
|
|
|
|
| 111 |
continue
|
| 112 |
blocknummer = re.sub(r'\D', '', subfolder.name)[:2].zfill(2)
|
| 113 |
|
|
|
|
| 114 |
brand_counters = defaultdict(int)
|
| 115 |
|
|
|
|
| 116 |
files_to_process = []
|
| 117 |
for file in sorted(subfolder.iterdir()):
|
| 118 |
if file.suffix.lower() not in ALL_ALLOWED_EXTENSIONS:
|
|
|
|
| 121 |
brand_counters[marke] += 1
|
| 122 |
files_to_process.append((file, marke))
|
| 123 |
|
|
|
|
| 124 |
brand_counters = defaultdict(int)
|
| 125 |
|
|
|
|
| 126 |
for file, marke in files_to_process:
|
| 127 |
brand_counters[marke] += 1 # Increment counter for this brand
|
| 128 |
count_str = f"{brand_counters[marke]:02d}" # Format as 01, 02, 03, etc.
|
|
|
|
| 130 |
markennummer = marken_index[marke]
|
| 131 |
cleaned = get_cleaned_filename_without_brand(file.name, marke)
|
| 132 |
|
|
|
|
| 133 |
neuer_name = f"{markennummer}B{blocknummer}{marke}{count_str}{cleaned}{file.suffix.lower()}"
|
| 134 |
ziel = output_root / neuer_name
|
| 135 |
|
| 136 |
if file.suffix.lower() in ALLOWED_IMAGE_EXTENSIONS:
|
| 137 |
with PILImage.open(file) as img:
|
|
|
|
| 138 |
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
|
|
|
|
| 139 |
img.save(ziel, format='PNG')
|
| 140 |
else:
|
|
|
|
| 141 |
img.convert("RGBA").save(ziel, format='PNG')
|
| 142 |
else:
|
| 143 |
shutil.copy2(file, ziel)
|
|
|
|
| 150 |
def is_valid_file(file_path):
|
| 151 |
"""Check if file should be processed - accept ALL files except hidden/system files"""
|
| 152 |
filename = Path(file_path).name
|
|
|
|
| 153 |
if filename.startswith('.') or filename.startswith('~') or filename == 'Thumbs.db':
|
| 154 |
return False
|
|
|
|
| 155 |
return True
|
| 156 |
|
| 157 |
def generate_excel_report(output_folder: Path, marken_index: dict, file_to_factorgroup: dict):
|
|
|
|
| 162 |
data = []
|
| 163 |
|
| 164 |
for file in sorted(output_folder.iterdir()):
|
|
|
|
| 165 |
if file.is_file() and is_valid_file(file):
|
| 166 |
name = file.stem
|
|
|
|
| 167 |
file_ext = file.suffix.lower() if file.suffix else ''
|
| 168 |
|
| 169 |
match = re.match(r"(\d{2})B(\d{2})([a-z0-9]+)", name, re.IGNORECASE)
|
|
|
|
| 171 |
markennummer, blocknummer, marke = match.groups()
|
| 172 |
factor = nummer_zu_marke.get(markennummer, marke)
|
| 173 |
|
|
|
|
| 174 |
factorgroup = file_to_factorgroup.get(file.name, f"{blocknummer}Unknown")
|
| 175 |
factorgroup = factorgroup.replace('_', '') # Remove all underscores
|
| 176 |
|
|
|
|
| 177 |
only_id = name
|
| 178 |
is_text_file = False
|
| 179 |
is_b20_id = False
|
| 180 |
|
|
|
|
| 181 |
if "B20" in only_id.upper():
|
| 182 |
is_b20_id = True
|
|
|
|
| 183 |
language_value = only_id + ".png"
|
| 184 |
elif file_ext == '.txt':
|
|
|
|
| 185 |
is_text_file = True
|
| 186 |
try:
|
|
|
|
| 187 |
with open(file, 'r', encoding='utf-8', errors='ignore') as txt_file:
|
| 188 |
txt_content = txt_file.read().strip()
|
|
|
|
| 189 |
language_value = txt_content if txt_content else "[Empty file]"
|
| 190 |
except Exception as e:
|
|
|
|
| 191 |
language_value = f"[Error reading file: {str(e)}]"
|
| 192 |
else:
|
|
|
|
| 193 |
language_value = name + file_ext
|
| 194 |
|
| 195 |
data.append({
|
|
|
|
| 197 |
"factorgroup": factorgroup,
|
| 198 |
"ID": only_id,
|
| 199 |
"Language": language_value,
|
| 200 |
+
"highlight_yellow": is_text_file or is_b20_id
|
| 201 |
})
|
| 202 |
|
| 203 |
|
|
|
|
| 205 |
|
| 206 |
reordered_data = []
|
| 207 |
for _, row in df_assets.iterrows():
|
| 208 |
+
raw_factor = str(row["factorgroup"])
|
| 209 |
+
group_prefix = raw_factor[:2]
|
| 210 |
try:
|
| 211 |
group = str(int(group_prefix))
|
| 212 |
except ValueError:
|
| 213 |
group = group_prefix
|
| 214 |
|
|
|
|
| 215 |
clean_factor = str(row["factorgroup"]).replace('_', '')
|
| 216 |
+
clean_factorgroup = str(row["factor"]).replace('_', '')
|
| 217 |
|
| 218 |
reordered_data.append({
|
| 219 |
"Group": group,
|
|
|
|
| 226 |
df_reordered = pd.DataFrame(reordered_data, columns=["Group", "factor", "factorgroup", "ID", "Language"])
|
| 227 |
df_reordered['factor'] = df_reordered['factor'].astype(str)
|
| 228 |
|
|
|
|
| 229 |
df_reordered['Group_num'] = pd.to_numeric(df_reordered['Group'], errors='coerce').fillna(999).astype(int)
|
| 230 |
|
|
|
|
| 231 |
df_reordered['factor_num'] = df_reordered['factor'].str.extract(r'^(\d+)')[0]
|
| 232 |
df_reordered['factor_num'] = pd.to_numeric(df_reordered['factor_num'], errors='coerce').fillna(999).astype(int)
|
| 233 |
|
|
|
|
| 234 |
df_reordered = df_reordered.sort_values(
|
| 235 |
by=['Group_num', 'factor_num', 'factor'],
|
| 236 |
ascending=[True, True, True]
|
| 237 |
).reset_index(drop=True)
|
| 238 |
|
|
|
|
| 239 |
df_reordered = df_reordered.drop(['factor_num', 'Group_num'], axis=1)
|
| 240 |
|
| 241 |
with pd.ExcelWriter(final_excel_path, engine='openpyxl') as writer:
|
| 242 |
df_assets.to_excel(writer, index=False, sheet_name="Assets")
|
| 243 |
df_reordered.to_excel(writer, index=False, sheet_name="Reordered")
|
| 244 |
|
|
|
|
| 245 |
workbook = writer.book
|
| 246 |
from openpyxl.styles import PatternFill
|
| 247 |
|
| 248 |
yellow_fill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid')
|
| 249 |
|
|
|
|
| 250 |
assets_sheet = workbook['Assets']
|
| 251 |
language_col_idx = 4 # Language is the 4th column (D)
|
| 252 |
for row_idx in range(2, len(df_assets) + 2): # Start from row 2 (after header)
|
| 253 |
cell = assets_sheet.cell(row=row_idx, column=language_col_idx)
|
|
|
|
| 254 |
if row_idx - 2 < len(data):
|
| 255 |
if data[row_idx - 2].get("highlight_yellow", False):
|
| 256 |
cell.fill = yellow_fill
|
| 257 |
|
|
|
|
| 258 |
reordered_sheet = workbook['Reordered']
|
| 259 |
language_col_idx = 5 # Language is the 5th column (E)
|
| 260 |
id_col_idx = 4 # ID is the 4th column (D)
|
| 261 |
|
|
|
|
| 262 |
id_to_highlight = {item["ID"]: item.get("highlight_yellow", False) for item in data}
|
| 263 |
|
| 264 |
for row_idx in range(2, len(df_reordered) + 2): # Start from row 2 (after header)
|
| 265 |
cell = reordered_sheet.cell(row=row_idx, column=language_col_idx)
|
|
|
|
| 266 |
id_cell = reordered_sheet.cell(row=row_idx, column=id_col_idx)
|
| 267 |
id_value = id_cell.value
|
| 268 |
if id_value and id_to_highlight.get(id_value, False):
|
|
|
|
| 271 |
return final_excel_path
|
| 272 |
|
| 273 |
|
|
|
|
| 274 |
def add_brand_pages_with_png_extensions(elements, marken_spalten, renamed_files_by_folder_and_marke, marken_index):
|
| 275 |
"""Add brand overview pages with .png extensions in labels"""
|
| 276 |
styles = getSampleStyleSheet()
|