# 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()