Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Olivier
Peer-graded student exercises
Commits
0d22741b
Commit
0d22741b
authored
Aug 15, 2019
by
Olivier
Browse files
M10: input in subfolder, raise exception if needed + cleanup
parent
bff6831a
Changes
1
Hide whitespace changes
Inline
Side-by-side
M10_process_grades/extract_grades.py
View file @
0d22741b
# -*- 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
(
p
ath
=
'.'
)
list_of_files
=
os
.
listdir
(
p
dfs_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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment