Untitled
import xlwings as xw import json def excel_to_json(file_path): # Open the Excel workbook wb = xw.Book(file_path) # Dictionary to store all tables all_tables = {} # Iterate through all sheets in the workbook for sheet in wb.sheets: # Get all tables in the current sheet tables = sheet.tables # Iterate through all tables in the current sheet for table in tables: table_name = f"{sheet.name}_{table.name}" data = table.range.options(dict, expand='table').value # Print the table print(f"Table: {table_name}") print(json.dumps(data, indent=2)) print("\n") # Add table data to the dictionary all_tables[table_name] = data # Close the workbook wb.close() # Save all tables as JSON with open('all_tables.json', 'w') as f: json.dump(all_tables, f, indent=2) print("All tables have been saved to 'all_tables.json'") # Usage excel_file_path = 'path/to/your/excel/file.xlsx' excel_to_json(excel_file_path)
Leave a Comment