PPaste!

doc 2 pdf

Home - All the pastes - Authored by Thooms

Raw version

 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
# doc_to_pdf.py (needs ms word to be installed)
from docx2pdf import convert  # pip instal docx2pdf
# Built-in libraries
import sys
import os
import re  


def main():
    input_dir = os.path.abspath(sys.argv[1])  # 2nd Argument from command
    output_dir = os.path.abspath(sys.argv[2])  # 3rd Argument from command

    if not os.path.exists(output_dir):
        # check if the output dir doesn't exist, if so, create one
        os.mkdir(output_dir)

    if os.path.exists(input_dir):
        # loop through all files in input_dir
        for fname in os.listdir(input_dir):
            # just get the files with .docx extension
            if fname.endswith('.docx'):
                pattern = r'(.*)\.'  # set a regex pattern
                # Search for the pattern in the name of the file
                data = re.search(pattern, fname)
                # convert to pdf and save in output_dir
                convert(fr'{input_dir}/{fname}', fr'{output_dir}/{data.group(1)}.pdf')
    else:
        # print that the input dir doesn't exist if it actually doesn't.
        print(input_dir + 'does not exists')


if __name__ == "__main__":
    main()