Batch convert PDF to WORD using Python

Batch convert PDF to WORD using Python

Scenario: you have 100’s of pdf files which needs to be converted to .docx format

Tools required :

Python

Packages : pip install pdf2docx

Keep all pdf files in src folders

create a dest folder and all converted docx files will appear here

Python Code:

from pdf2docx import Converter
import os

# Specifying the pdf & docx files

def allfiles():
    p = os.listdir('src')
    for m in p:    
        pdf_file = m
        docx_file = m.replace(".pdf",".docx")
        condoc('src/'+pdf_file, 'dest/'+docx_file)

def condoc(pdf_file, docx_file):
    try:
        # Converting PDF to Docx
        cv_obj = Converter(pdf_file)
        cv_obj.convert(docx_file)
        cv_obj.close()

    except:
        print('Conversion Failed')
    
    else:
        print('File Converted Successfully')

allfiles()