File: txt2rtf.py

package info (click to toggle)
pkgconf 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,820 kB
  • sloc: ansic: 8,111; sh: 6,689; makefile: 247; python: 157
file content (35 lines) | stat: -rw-r--r-- 1,201 bytes parent folder | download
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)