-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
58 lines (44 loc) · 1.55 KB
/
script.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
import sys
import os
import json
import re
import unicodedata
import subprocess
data_path="data.json"
pdf_file_name="file.pdf"
latex_file_name="latex.tex"
text_file_name="text.txt"
def latex_create_pdf(text, pdf_file_name):
# Generate LaTeX code with added packages
latex_content = (
"\\documentclass{article}\n"
"\\usepackage[T1]{fontenc}\n"
"\\usepackage[utf8]{inputenc}\n"
"\\usepackage{lmodern}\n"
"\\begin{document}\n"
"\\begin{verbatim}\n"
)
# Escape special characters
escaped_text = re.sub(r'([#$%&_{}])', r'\\\1', text)
latex_content += escaped_text
latex_content += "\\end{verbatim}"
latex_content += "\\end{document}"
# Write LaTeX content to a file
with open(latex_file_name, "wb") as f:
f.write(latex_content.encode('utf-8'))
# Get the directory of the current script
script_dir = os.path.dirname(os.path.realpath(__file__))
# Specify the name of the PDF file
pdf_file_name = os.path.join(script_dir, pdf_file_name)
try:
subprocess.run(['pdflatex', latex_file_name], check=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred: {e}")
if __name__ == "__main__":
with open(data_path, 'r', encoding='utf-8') as file:
json_data = json.load(file)
json_formatted_str = json.dumps(json_data, indent=2)
with open(text_file_name, "w") as file:
file.write(json_formatted_str)
# Generate the PDF file
latex_create_pdf(json_formatted_str, pdf_file_name) #LaTeX