Commit 0d22741b authored by Olivier's avatar Olivier
Browse files

M10: input in subfolder, raise exception if needed + cleanup

parent bff6831a
# -*- coding: utf-8 -*-
"""
Author: Germán Santa Maria
Author: Germán Santa-Maria
Script: Process Grades
Description:
This script will read all pdf files in the folder where is contained and
will produce a CSV file with the values extracted from the all fillable
text boxes in each of the pdfs.
This script will take a filled PDF forms as input, and will produce
a CSV file with the values extracted from the information contained
in the forms
Conditions:
All the pdfs should come from the same form template (same number of
......@@ -15,8 +14,11 @@ Conditions:
"""
"""-------------------------Hard coded input--------------------------------"""
# Output file name
csv_file_name = 'script_output/grades.csv' # Output csv file name (to store grades)
# Input file path
pdfs_path = 'script_input/pdfs/' # pdf files path
# Output file path
csv_file_name = 'script_output/grades.csv' # csv file name (to store grades)
"""-------------------------------------------------------------------------"""
# Import modules
......@@ -25,7 +27,7 @@ import PyPDF2 # For working with PDFs
import csv # For reading cvs files
# List of all the files in the directory
list_of_files = os.listdir(path='.')
list_of_files = os.listdir(pdfs_path)
# Filter only ".pdf" files
list_of_pdf_files = []
......@@ -33,12 +35,15 @@ for i in range(len(list_of_files)) :
if list_of_files[i].endswith(".pdf"):
list_of_pdf_files.append(list_of_files[i])
if len(list_of_pdf_files) == 0:
raise Exception("No pdf files were found in the selected path")
#---------------Create the headers for the output csv file--------------------#
# Select first pdf in the list as calibration pdf
calibration_pdf = list_of_pdf_files[0]
# Read PDF file
object = open(calibration_pdf, 'rb') # rb means read binary
object = open(pdfs_path + calibration_pdf, 'rb') # rb means read binary
reader = PyPDF2.PdfFileReader(object)
# Get values from text boxes in the PDF
......@@ -58,7 +63,7 @@ errors = [] # List of pdfs that could not be processed
for pdf_file in list_of_pdf_files:
try:
# Read PDF file
object = open(pdf_file, 'rb') # rb means read binary
object = open(pdfs_path + pdf_file, 'rb') # rb means read binary
reader = PyPDF2.PdfFileReader(object)
# Get values from text boxes in the PDF
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment