-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONtoXLSX.py
More file actions
138 lines (117 loc) · 6.79 KB
/
JSONtoXLSX.py
File metadata and controls
138 lines (117 loc) · 6.79 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
import os
import sys
import subprocess
import importlib
import json
import argparse
# ─────────────────────────────────────────────────────────────────────────────
# macOS dialog helpers using AppleScript
# ─────────────────────────────────────────────────────────────────────────────
def macos_prompt(message, title="Python Script"):
script = f'display dialog "{message}" with title "{title}" buttons {{"No", "Yes"}} default button "Yes"'
try:
result = subprocess.run(["osascript", "-e", script], capture_output=True, text=True)
return "yes" in result.stdout.lower()
except Exception as e:
print(f"Failed to show dialog: {e}")
return False
def macos_choose_file():
script = '''
set theFile to choose file with prompt "Select your input JSON file"
POSIX path of theFile
'''
try:
result = subprocess.run(['osascript', '-e', script], capture_output=True, text=True)
return result.stdout.strip()
except Exception as e:
print(f"Error choosing file: {e}")
return None
def macos_choose_folder():
script = '''
set theFolder to choose folder with prompt "Select a folder to save the Excel file"
POSIX path of theFolder
'''
try:
result = subprocess.run(['osascript', '-e', script], capture_output=True, text=True)
return result.stdout.strip()
except Exception as e:
print(f"Error choosing folder: {e}")
return None
# ─────────────────────────────────────────────────────────────────────────────
# Package check and install
# ─────────────────────────────────────────────────────────────────────────────
def check_and_install(package_name, description=""):
try:
importlib.import_module(package_name)
except ImportError:
should_install = False
if sys.platform == "darwin":
should_install = macos_prompt(
f"'{package_name}' is not installed and is required for this script.\n{description}\nWould you like to install it?",
title="JSON to Excel"
)
else:
response = input(f"'{package_name}' is not installed.\n{description}\nInstall it now? (y/n): ").strip().lower()
should_install = response in ['y', 'yes']
if should_install:
print(f"Installing {package_name}...")
subprocess.check_call([sys.executable, "-m", "pip", "install", package_name])
else:
print(f"Cannot continue without '{package_name}'. Exiting.")
sys.exit(1)
check_and_install("pandas", "Pandas helps manipulate and analyze data in Python, offering powerful data structures and easy file handling.")
check_and_install("openpyxl", "OpenPyXL handles the creation and manipulation of Excel files, enabling .xlsx output.")
import pandas as pd
import openpyxl
# ─────────────────────────────────────────────────────────────────────────────
# Parse CLI arguments
# ─────────────────────────────────────────────────────────────────────────────
parser = argparse.ArgumentParser(description="Convert JSON to Excel")
parser.add_argument('--input', '-i', help='Path to input JSON file')
parser.add_argument('--output', '-o', help='Path to output Excel file')
args = parser.parse_args()
input_file = args.input
output_file = args.output
# ─────────────────────────────────────────────────────────────────────────────
# Interactive fallbacks
# ─────────────────────────────────────────────────────────────────────────────
if not input_file:
if sys.platform == 'darwin':
input_file = macos_choose_file()
else:
input_file = input("Enter path to input JSON file: ").strip()
if not input_file or not os.path.isfile(input_file):
print("❌ Invalid or missing input file. Exiting.")
sys.exit(1)
if not output_file:
base_name = os.path.splitext(os.path.basename(input_file))[0]
if sys.platform == 'darwin':
output_folder = macos_choose_folder()
if not output_folder:
output_folder = os.getcwd()
else:
output_folder = os.getcwd()
output_file = os.path.join(output_folder, f"{base_name}.xlsx")
# ─────────────────────────────────────────────────────────────────────────────
# Convert JSON to Excel
# ─────────────────────────────────────────────────────────────────────────────
try:
with open(input_file, 'r') as f:
data = json.load(f)
df = pd.DataFrame(data)
df.to_excel(output_file, index=False)
print(f"✅ Excel file created: {output_file}")
except Exception as e:
print(f"❌ Failed to convert JSON to Excel: {e}")
sys.exit(1)
# ─────────────────────────────────────────────────────────────────────────────
# Open output folder
# ─────────────────────────────────────────────────────────────────────────────
folder_path = os.path.dirname(os.path.abspath(output_file))
if sys.platform == 'darwin':
os.system(f"open '{folder_path}'")
elif sys.platform == 'linux':
subprocess.run(['xdg-open', folder_path])
elif sys.platform == 'win32':
os.startfile(folder_path)