File size: 2,543 Bytes
a555f47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import json
from pathlib import Path
from xls_to_xlsx import convert_xls_to_xlsx

# Set up paths
BASE_DIR = Path(__file__).parent
ORIGINAL_DATA = BASE_DIR / 'original_data'
CLEAN_DATA =  'clean_data'
ANNOTATIONS_FILE = ORIGINAL_DATA / 'Table range annotations.txt'

# Create clean_data directory if it doesn't exist

def clean_path(path):
    """Convert Windows path to a clean filename."""
    return path.replace('\\', '_').replace('/', '_')

def process_table_regions(regions):
    """Convert table regions string into a list of regions."""
    return [region.strip() for region in regions if region.strip()]

def process_annotations():
    # List to store JSONL entries
    jsonl_entries = []
    
    # Process each line
    with open(ANNOTATIONS_FILE, 'r', encoding='utf-8') as f:
        for line in f:
            # Split line by tabs
            parts = line.strip().split('\t')
            if len(parts) < 4:  # Need at least filename, sheet, split, folder
                continue
                
            file_name = parts[0]
            sheet_name = parts[1]
            split = parts[2]
            file_folder = parts[3]
            table_regions = parts[4:] if len(parts) > 4 else []
            
            original_path = os.path.join(ORIGINAL_DATA, file_folder, file_name)  # ORIGINAL_DATA  file_folder / file_name
            
            # Create clean filename using the full path
            clean_filename = clean_path(f"{file_folder}_{file_name}")
            
            converted_path = original_path.replace('.xlsx', '.xls').replace('\\','/')
            print(converted_path)
            convert_xls_to_xlsx(converted_path, CLEAN_DATA)
            
            #rename the file to the clean filename
            os.rename(os.path.join(CLEAN_DATA, file_name), os.path.join(CLEAN_DATA, clean_filename))
            
            # Create entry for JSONL
            entry = {
                'original_file': str(file_name),
                'original_path': str(file_folder),
                'clean_file': str(clean_filename),
                'sheet_name': str(sheet_name),
                'split': str(split),
                'table_regions': process_table_regions(table_regions)
            }
            
            jsonl_entries.append(entry)
    
    # Write to JSONL file
    output_file = BASE_DIR / 'annotations.jsonl'
    with open(output_file, 'w') as f:
        for entry in jsonl_entries:
            f.write(json.dumps(entry) + '\n')

if __name__ == '__main__':
    process_annotations()