This repository was archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathxlsform.py
41 lines (40 loc) · 1.71 KB
/
xlsform.py
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
"""
Drag and drop a file onto this exe in order to convert it.
The xform will appear in an output folder in the same directory as this exe.
"""
import os, sys
import pyxform
from pyxform.utils import sheet_to_csv, has_external_choices
if __name__ == '__main__':
try:
argv = sys.argv
if len(argv) < 2:
print __doc__
print 'Usage:'
print argv[0] + ' path_to_XLSForm'
else:
warnings = []
name, ext = os.path.splitext(os.path.basename(argv[1]))
output_dir = os.path.join(os.path.split(argv[0])[0], 'output')
out_file = os.path.join(output_dir, name + '.xml')
if not os.path.exists(output_dir):
os.makedirs(output_dir)
json_survey = pyxform.xls2json.parse_file_to_json(argv[1], warnings=warnings)
survey = pyxform.builder.create_survey_element_from_dict(json_survey)
survey.print_xform_to_file(out_file, validate=False)
if has_external_choices(json_survey):
itemsets_csv = os.path.join(output_dir, "itemsets.csv")
choices_exported = sheet_to_csv(argv[1], itemsets_csv, "external_choices")
if not choices_exported:
print "Could not export itemsets.csv, perhaps the external choices sheet is missing."
else:
print 'external choices csv is located at:', itemsets_csv
if len(warnings) > 0:
print "Warnings:"
for w in warnings:
print w
print 'xform is located at:', out_file
print 'Conversion complete!'
except Exception as e:
print e
raw_input("Press Enter to continue...")