forked from pkgconf/pkgconf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxt2rtf.py
More file actions
35 lines (25 loc) · 1.17 KB
/
txt2rtf.py
File metadata and controls
35 lines (25 loc) · 1.17 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
def text_to_rtf(input_file: str, output_file: str) -> None:
with open(input_file, "r", encoding="utf-8") as file:
text_content = file.read()
text_content = text_content.replace("\\", "\\\\")
text_content = text_content.replace("{", "\\{")
text_content = text_content.replace("}", "\\}")
text_content = text_content.replace("\n", "\\par\n")
rtf_content = "{\\rtf1\\ansi\\ansicpg1252\\cocoartf2580\\cocoasubrtf220\n"
rtf_content += "{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}\n"
rtf_content += "\\vieww12000\\viewh15840\\viewkind0\n"
rtf_content += "\\pard\\tx720\\tx1440\\tx2160\\tx2880\\tx3600\\tx4320\\pardirnatural\\partightenfactor0\n"
rtf_content += "\\f0\\fs24 "
rtf_content += text_content
rtf_content += "\n}"
with open(output_file, "w", encoding="utf-8") as file:
file.write(rtf_content)
print(f"Conversion complete! RTF file saved as: {output_file}")
if __name__ == "__main__":
import sys
if len(sys.argv) != 3:
print(f"Usage: python {sys.argv[0]} input.txt output.rtf")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
text_to_rtf(input_file, output_file)