-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
100 lines (80 loc) · 2.84 KB
/
main.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
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
# -*- coding: utf-8 -*-
# @Time : 2020/9/27 18:40
# @Author : Enmo Ren
# @FileName: main.py
# @Software: PyCharms
import json
import pandas
from json2xml import json2xml
import geojson
from error_handler import error_exit
from pdf_format import PDF
from io import StringIO
def JSONtoPDF(filename: str, json_data: dict):
# Get the data values from the JSON string json_data.
try:
data = json.loads(json_data)
except Exception as e:
error_exit("Invalid JSON data: {}".format(e.message))
try:
pdf = PDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
for i, line in enumerate(data):
content = f"{line}:{data[line]}"
pdf.cell(200, 10, txt=content, ln=i, align="l")
pdf.output(filename)
except Exception as e:
error_exit("Error while generating PDF file: {}".format(e.message))
def testJSONtoPDF(filename: str, json_content: str):
json_data = json.dumps(json_content)
JSONtoPDF(filename, json_data)
def JSONtoTXT(filename: str, json_data: dict):
try:
data = json.loads(json_data)
except Exception as e:
error_exit("Invalid JSON data: {}".format(e.message))
try:
with open(filename, 'w') as outfile:
json.dump(data, outfile)
except Exception as e:
error_exit("Error while generating TXT file: {}".format(e.message))
def testJSONtoTXT(filename: str, json_content: str):
json_data = json.dumps(json_content)
JSONtoTXT(filename, json_data)
def JSONtoXML(filename: str, json_content: str):
try:
data = json.loads(json_content)
except Exception as e:
error_exit("Invalid JSON data: {}".format(e.message))
data = json2xml.Json2xml(data).to_xml()
try:
with open(filename, 'w') as outfile:
json.dump(data, outfile)
except Exception as e:
error_exit("Error while generating TXT file: {}".format(e.message))
def testJSONtoXML(filename: str, json_content: str):
json_data = json.dumps(json_content)
JSONtoXML(filename, json_data)
def JSONtoCSV(filename: str, json_content: str):
data = pandas.read_json(StringIO(json_content), typ='series')
return data.to_csv(filename)
def testJSONtoCSV(filename: str, json_content: str):
json_data = json.dumps(json_content)
JSONtoCSV(filename, json_data)
def main():
json_test = {
'pdf_filename': 'placeholder',
'font_name': 'Courier',
'font_size': 12,
'header': 'The Man in the Arena',
'footer': 'Generated by xtopdf - http://google.com/search?q=xtopdf',
'lines': 'list_placeholder'
}
# testJSONtoPDF('test.pdf', json_test)
# testJSONtoTXT('test.txt', json_test)
# testJSONtoXML('test.xml', json_test)
testJSONtoCSV('test.csv', json_test)
if __name__ == '__main__':
main()
# y_point = geojson.Point((43.24, -1.532))