-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_to_word.py
More file actions
64 lines (48 loc) · 2.42 KB
/
export_to_word.py
File metadata and controls
64 lines (48 loc) · 2.42 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
import json
from pathlib import Path
from docx import Document
OUTPUT_DIR = Path("course_lessons/output")
WORD_DOC_DIR = Path("course_lessons/word_docs")
def create_word_docs_by_module():
"""Create Word documents for each module using JSON lesson content."""
# Ensure the Word document directory exists
WORD_DOC_DIR.mkdir(parents=True, exist_ok=True)
# Iterate through each module folder
for module_folder in OUTPUT_DIR.iterdir():
if not module_folder.is_dir():
continue # Skip non-directory files
# Create a Word document for the current module
module_name = module_folder.name
word_output_file = WORD_DOC_DIR / f"{module_name}.docx"
doc = Document()
doc.add_heading(f"{module_name.capitalize()}", level=1)
# Process all JSON lesson files in the module folder
for lesson_file in sorted(module_folder.glob("*.json")):
lesson_name = lesson_file.stem.replace("_", " ").title()
doc.add_heading(lesson_name, level=2)
# Convert JSON content into Word paragraphs
with open(lesson_file, "r") as file:
lesson_data = json.load(file)
# Add sections based on JSON keys
if "getting_started" in lesson_data:
doc.add_heading("Getting Started", level=3)
doc.add_paragraph(lesson_data["getting_started"])
if "background_information" in lesson_data:
doc.add_heading("Background Information", level=3)
doc.add_paragraph(lesson_data["background_information"])
if "learning_outcomes" in lesson_data:
doc.add_heading("Learning Outcomes", level=3)
for outcome in lesson_data["learning_outcomes"]:
doc.add_paragraph(f"- {outcome}")
if "instructions" in lesson_data:
doc.add_heading("Instructions", level=3)
doc.add_paragraph(lesson_data["instructions"])
if "resources" in lesson_data and lesson_data["resources"]:
doc.add_heading("Resources", level=3)
for resource in lesson_data["resources"]:
doc.add_paragraph(f"- {resource}")
# Save the Word document for the module
doc.save(word_output_file)
print(f"Saved Word document for {module_name} at: {word_output_file}")
if __name__ == "__main__":
create_word_docs_by_module()