export_xls_to_json.py 1.7 KB
Newer Older
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
import argparse
import os
import pathlib
from metadata_tools.importxls.dish_xls_exporter import DishXlsExporter
from metadata_tools.importxls.export_utils import save_exported_datasets_to_file


parser = argparse.ArgumentParser(description='Load XLS file and export as JSON')

parser.add_argument(
    '-f',
    '--file',
    nargs='+',
    help='Path to XLS/XLSX file to parse',
    default=False)

parser.add_argument(
    '-d',
    '--directory',
    help='Directory with XLS/XLSX files',
    default=False,
    dest='dirname')

parser.add_argument(
    '-o',
    '--output-dir',
27
28
    help='Path to the directory to store JSON files to.',
    required=True,
29
30
31
32
    dest="output_dir"
)

args = parser.parse_args()
33
34
if not (args.dirname or args.file):
    parser.error('No arguments provided. Either -d or -f must be specified')
35
36
37
38
39
40

fileList = []

if args.dirname:
    dirname = args.dirname
    for f in os.listdir(dirname):
Vilem Ded's avatar
Vilem Ded committed
41
42
43
        filename = os.path.join(dirname, f)
        if os.path.isfile(filename):
            fileList.append(filename)
44
45

if args.file:
Vilem Ded's avatar
Vilem Ded committed
46
47
48
49
50
    for f in args.file:
        if os.path.isfile(f):
            fileList.append(f)
        else:
            print(f"'{f}' is not file - skipping")
51
52


Vilem Ded's avatar
Vilem Ded committed
53
54
exporter = DishXlsExporter()
counter = 0
55
for fname in fileList:
Vilem Ded's avatar
Vilem Ded committed
56
57
58
59
60
    try:
        dataset_dict = exporter.export_submission(fname)
    except Exception as e:
        raise Exception(f'Error occured in file {fname}') from e

61
62
63
64
    fname_json = pathlib.Path(fname).stem + ".json"
    fname_out = os.path.join(args.output_dir, fname_json)
    with open(fname_out, 'w') as outfile:
        save_exported_datasets_to_file(dataset_dict, outfile)
Vilem Ded's avatar
Vilem Ded committed
65
66
67
    counter += 1

print(f'File export successful: {counter} files exported to {args.output_dir}')