-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.py
69 lines (54 loc) · 1.9 KB
/
print.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
from docx import Document
from docx.shared import RGBColor
import os
def set_custom_background_color(doc, color_rgb):
section = doc.sections[-1]
paragraph = doc.add_paragraph()
paragraph.alignment = 3 # Right-aligned
shape = paragraph.add_run().add_shape(
1, # Shape type: Rectangle
section.page_width, section.page_height
)
fill = shape.fill
fill.solid()
fill.fore_color.rgb = color_rgb
def print_json(obj: dict):
if not os.path.exists('docs'):
os.makedirs('docs')
title = obj['title']
sanitized_title = "".join(x for x in title if (x.isalnum() or x in "._- "))
filename = f"{sanitized_title}.docx"
file_path = os.path.join('docs', filename)
doc = Document()
# set_custom_background_color(doc, RGBColor(100, 89, 77))
doc.add_heading(title, level=1)
with open('text.txt', 'r') as f:
text = f.read()
doc.add_heading('Text', level=2)
doc.add_paragraph(text)
summary = obj.get('summary', '')
doc.add_heading('Summary', level=2)
doc.add_paragraph(summary)
questions = obj.get('questions', [])
for question in questions:
questionType = question.get('questionType', '')
questionText = question.get('question', '')
answerText = question.get('answer', '')
doc.add_heading(questionType, level=3)
doc.add_heading('Question', level=3)
doc.add_paragraph(questionText)
doc.add_heading('Answer', level=3)
doc.add_paragraph(answerText)
hots = obj.get('HOTS', [])
doc.add_heading('Higher Order Thinking Skills', level=2)
for topic in hots:
doc.add_paragraph(topic)
doc.save(file_path)
# Example usage
if __name__ == "__main__":
import json
print("Reading response.json")
with open('response.json', 'r') as f:
res = json.load(f)
print(res)
print_json(res)