-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc2pdf.py
42 lines (30 loc) · 1.13 KB
/
doc2pdf.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
#!/usr/bin/env python3
"""
Single file standalone script to convert a text/document file to a PDF file using LibreOiffice.
"""
import subprocess
import shutil
import os
import sys
from pathlib import Path
import argparse
def get_lo_exe() -> str:
name = "soffice"
path = None
match sys.platform:
case "win32":
path = Path(os.environ["PROGRAMFILES"]) / "LibreOffice/program"
case "darwin":
path = Path("/Applications/LibreOffice.app/Contents/MacOS")
if not (exe := shutil.which(name, path=path)):
raise FileNotFoundError("LibreOffice not found")
return exe
if __name__ == "__main__":
p = argparse.ArgumentParser(description="Convert a document file to PDF using LibreOffice")
p.add_argument("filein", help="Input file")
p.add_argument("out_dir", help="Output directory", nargs="?")
args = p.parse_args()
filein = Path(args.filein).expanduser().resolve()
out_dir = Path(args.out_dir).expanduser().resolve() if args.out_dir else filein.parent
cmd = [get_lo_exe(), "--convert-to", "pdf", "--outdir", str(out_dir), str(filein)]
subprocess.check_call(cmd)