|
import os |
|
import json |
|
from pathlib import Path |
|
from xls_to_xlsx import convert_xls_to_xlsx |
|
|
|
|
|
BASE_DIR = Path(__file__).parent |
|
ORIGINAL_DATA = BASE_DIR / 'original_data' |
|
CLEAN_DATA = 'clean_data' |
|
ANNOTATIONS_FILE = ORIGINAL_DATA / 'Table range annotations.txt' |
|
|
|
|
|
|
|
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(): |
|
|
|
jsonl_entries = [] |
|
|
|
|
|
with open(ANNOTATIONS_FILE, 'r', encoding='utf-8') as f: |
|
for line in f: |
|
|
|
parts = line.strip().split('\t') |
|
if len(parts) < 4: |
|
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) |
|
|
|
|
|
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) |
|
|
|
|
|
os.rename(os.path.join(CLEAN_DATA, file_name), os.path.join(CLEAN_DATA, clean_filename)) |
|
|
|
|
|
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) |
|
|
|
|
|
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() |