2024-12-11 15:46:49 +00:00
import os
import csv
import glob
from PyPDF2 import PdfReader
from concurrent . futures import ThreadPoolExecutor , as_completed
2024-12-17 11:18:41 +00:00
"""
This script will collect the following information for all PDF files in a directory and its subdirectories:
- File Name
- File Path
- Page Count
The information will be saved in a CSV file.
NOTE: This script is required mainly in the pre-doczy analysis. There might not be need for this since CNC project is already processed through the textract pipeline.
"""
2024-12-11 15:46:49 +00:00
def get_pdf_info ( pdf_path ) :
try :
with open ( pdf_path , ' rb ' ) as file :
pdf_reader = PdfReader ( file )
page_count = len ( pdf_reader . pages )
except Exception as e :
print ( f " Error reading { pdf_path } : { e } " )
page_count = None
return os . path . basename ( pdf_path ) , pdf_path , page_count
def collect_pdf_info ( directory , output_csv ) :
pdf_files = glob . glob ( os . path . join ( directory , ' ** ' , ' *.pdf ' ) , recursive = True )
with open ( output_csv , mode = ' w ' , newline = ' ' , encoding = ' utf-8 ' ) as csv_file :
csv_writer = csv . writer ( csv_file )
csv_writer . writerow ( [ ' File Name ' , ' File Path ' , ' Page Count ' ] )
with ThreadPoolExecutor ( ) as executor :
futures = { executor . submit ( get_pdf_info , pdf ) : pdf for pdf in pdf_files }
for future in as_completed ( futures ) :
result = future . result ( )
if result :
csv_writer . writerow ( result )
print ( f " CSV file ' { output_csv } ' created successfully. " )
directory = ' T:/AArete Client Work/Doczy-Production/Restricted/icertis_contracts '
output_csv = ' icertis/icertis_files.csv '
collect_pdf_info ( directory , output_csv )