-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconvert.py
executable file
·50 lines (39 loc) · 1.25 KB
/
convert.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
#!/usr/bin/env python3
import os
import shutil
from glob import glob
from subprocess import PIPE, Popen
SOURCE_DIR = "díla"
OUTPUT_DIR = "pdf_output"
# check that pandoc is installed
if shutil.which("pandoc") is None:
raise Exception(f"Pandoc není nainstalovaný/spustitelný!")
# move working directory to this file's
os.chdir(os.path.abspath(os.path.dirname(__file__)))
# ensure the output dir exists
if not os.path.isdir(OUTPUT_DIR):
os.mkdir(OUTPUT_DIR)
# go through markdown files in the current directory
for file_path in sorted(glob(os.path.join(SOURCE_DIR, "*.md"))):
print(f"Převádím '{file_path}'")
file_name = os.path.basename(file_path)
file_name_noext = os.path.splitext(file_name)[0]
# skip README.md
if file_name.lower() == "README.md".lower():
continue
# execute the command
process = Popen(
[
"pandoc",
file_path,
"-o",
f"{os.path.join(OUTPUT_DIR, file_name_noext + '.pdf')}",
"-V",
"geometry:margin=1.5cm",
],
stdout=PIPE,
stderr=PIPE,
)
stdout, stderr = process.communicate()
if process.returncode != 0:
raise Exception(f"Při běhu Pandocu nastala chyba: {stderr.decode()}")