import sys
from bs4 import BeautifulSoup

html_file_path = 'table.html'
tsv_file_path = 'table.tsv'

def process_svg_for_rects(svg_element):
    """
    Process an SVG element to extract rect data associated with "-35" and "-10" text.
    Returns a dictionary with keys: w35, x35, w10, x10
    """
    rect_data = {'w35': "NONE", 'x35': "NONE", 'w10': "NONE", 'x10': "NONE"}
    
    if not svg_element: # Guard clause if None is passed
        return rect_data
        
    rects = svg_element.find_all('rect')
    for rect in rects:
        next_text = rect.find_next('text')
        if next_text:
            text_content = next_text.get_text(strip=True)
            if text_content == "-35":
                rect_data['w35'] = rect.get('width', "NONE")
                rect_data['x35'] = rect.get('x', "NONE")
            elif text_content == "-10":
                rect_data['w10'] = rect.get('width', "NONE")
                rect_data['x10'] = rect.get('x', "NONE")
    return rect_data

try:
    with open(html_file_path, 'r', encoding='utf-8') as f:
        html_content = f.read()
except FileNotFoundError:
    print(f"Error: Input file '{html_file_path}' not found.", file=sys.stderr)
    sys.exit(1)
except Exception as e:
    print(f"Error reading file '{html_file_path}': {e}", file=sys.stderr)
    sys.exit(1)

try:
    soup = BeautifulSoup(html_content, 'lxml')
except Exception as e:
    print(f"Error parsing HTML: {e}", file=sys.stderr)
    sys.exit(1)

output_tsv_lines = []
html_tables = soup.find_all('table')

if not html_tables:
    print("No <table> elements found.", file=sys.stderr)
    sys.exit(1) # Consistent with original behavior

for table_idx, html_table_element in enumerate(html_tables):
    if len(html_tables) > 1:
        output_tsv_lines.append(f"# --- Data from Table {table_idx + 1} ---")
    else:
        output_tsv_lines.append(f"# --- Combined Table and Rect Data ---")

    final_table_headers = []
    # Stores tuples of (list_of_cell_texts, rect_data_dict_for_that_row)
    output_data_entries = [] 
    
    max_cols_in_html_table = 0 # Tracks max columns found in HTML part (header or data)

    html_table_rows = html_table_element.find_all('tr')
    is_first_tr_being_processed = True 

    for tr_element in html_table_rows:
        current_row_cells_text = []
        is_current_tr_a_header_row = False 
        
        cells = tr_element.find_all(['th', 'td'])

        # Process SVG for the current tr_element, regardless of cells
        svg_element_in_row = None
        for svg_tag in tr_element.find_all('svg'):
            if svg_tag.get('width') == '648' and svg_tag.get('height') == '50':
                svg_element_in_row = svg_tag
                break
        
        current_row_rect_data = process_svg_for_rects(svg_element_in_row) 

        if not cells: # Row has no <th> or <td> cells
            # Treat as a data row with no HTML cell content.
            # It still contributes its rect_data.
            output_data_entries.append(([], current_row_rect_data))
            # If this is the first row and it's empty, final_table_headers remains []
            # and max_cols_in_html_table remains 0. These might be updated by subsequent rows.
            is_first_tr_being_processed = False
            continue

        for cell_element in cells:
            if cell_element.name == 'th':
                is_current_tr_a_header_row = True
            cell_text = cell_element.get_text(separator=" ", strip=True)
            cell_text = cell_text.replace('\n', ' ').replace('\r', '').replace('\t', ' ')
            current_row_cells_text.append(cell_text)

        if is_first_tr_being_processed and is_current_tr_a_header_row:
            # This is the first <tr> and it's a header row
            final_table_headers = list(current_row_cells_text)
            max_cols_in_html_table = len(final_table_headers)
            # Rect data from this header row (current_row_rect_data) is captured but
            # not added to output_data_entries as this is a header, not a data row.
        else:
            # This is a data row. Either:
            # 1. Not the first <tr> processed.
            # 2. Is the first <tr> processed, but not a header row (no <th>).
            if is_first_tr_being_processed: # Implies: first <tr> AND not a header row
                # No explicit headers from <th>, so create empty headers based on this first data row's columns
                final_table_headers = [''] * len(current_row_cells_text)
                max_cols_in_html_table = len(current_row_cells_text)
            
            output_data_entries.append((current_row_cells_text, current_row_rect_data))
            max_cols_in_html_table = max(max_cols_in_html_table, len(current_row_cells_text))
            
            # Ensure final_table_headers (if derived from an earlier, narrower header/data row,
            # or if it was empty because the first row was data)
            # is padded to match the widest data row encountered so far.
            if len(final_table_headers) < max_cols_in_html_table:
                final_table_headers.extend([''] * (max_cols_in_html_table - len(final_table_headers)))
        
        is_first_tr_being_processed = False

    # Combine HTML headers with SVG data headers
    combined_tsv_headers = list(final_table_headers) 
    combined_tsv_headers.extend(["width-35", "x-35", "width-10", "x-10"])
    output_tsv_lines.append('\t'.join(combined_tsv_headers))

    # Create output data rows from collected entries
    for html_cell_list, rect_data_dict in output_data_entries:
        current_output_row_parts = []
        
        # Add HTML table cell data
        current_output_row_parts.extend(html_cell_list)
        # Pad HTML part if it's shorter than max_cols_in_html_table
        if len(html_cell_list) < max_cols_in_html_table:
            current_output_row_parts.extend([''] * (max_cols_in_html_table - len(html_cell_list)))

        # Add Rect data part
        w35_val = rect_data_dict.get('w35', "NONE")
        x35_val = rect_data_dict.get('x35', "NONE")
        w10_val = rect_data_dict.get('w10', "NONE")
        x10_val = rect_data_dict.get('x10', "NONE")
        
        current_output_row_parts.extend([str(w35_val), str(x35_val), str(w10_val), str(x10_val)])
        output_tsv_lines.append('\t'.join(current_output_row_parts))
    
    output_tsv_lines.append("") # Add an empty line as a separator if multiple tables

# Write output
if not output_tsv_lines:
    # This path should not be taken if html_tables had content, as headers etc. are added.
    # For safety, ensure an empty file is created if, for some reason, it's empty.
    with open(tsv_file_path, 'w', encoding='utf-8') as f:
        pass 
    print(f"Created empty file '{tsv_file_path}' (no content generated).")
else:
    # Remove trailing empty lines (which are used as separators between tables)
    while output_tsv_lines and output_tsv_lines[-1] == "":
        output_tsv_lines.pop()
    
    try:
        with open(tsv_file_path, 'w', encoding='utf-8') as f:
            if output_tsv_lines: 
                f.write('\n'.join(output_tsv_lines))
                f.write('\n') # Add a final newline character, consistent with original
        print(f"Successfully wrote to '{tsv_file_path}'.")
    except Exception as e:
        print(f"Error writing output file: {e}", file=sys.stderr)
        sys.exit(1)

Make it so it can select input and output file. Keep defaults the same