Commit b3f919b2 authored by Olivier's avatar Olivier
Browse files

M10: Add backup shell script to extract PDF form information

This script was developed to cater for cases where the python script
did not manage to read the content of the PDF forms submitted by
participants. It is no longer needed. It is added here for future
reference, and will be removed in next commit
parent 0d22741b
#!/bin/bash
# Input parameters
inputfolder='script_input'
outputfolder='script_output'
# Cleanup output folder
mkdir -v script_output.old
mv -vf $outputfolder/* script_output.old/
# Copy input into the output folder, change directory to output folder
rsync -avh --delete $inputfolder/ $outputfolder/
mkdir -v $outputfolder/failed
cd $outputfolder
# Pick up all the files in input subfolder one by one
for returnpdf in return/*.pdf; do
# Extract filerootname code from submission pdf file name
filerootname=$(echo "$returnpdf" | sed -r 's/return\///' | sed -r 's/\.pdf//')
echo "#"
echo "#"
echo "extracting data from PDF for $filerootname …"
# Grab the PDF
mv -v return/$filerootname.pdf ./
# Extract last page of pdf
# Note that pdftk is not available by default on Ubuntu 18.04,
# see https://askubuntu.com/a/1046476 for workaround install
pdftk $filerootname.pdf cat r1 output lastpage.pdf
echo $filerootname > $filerootname.txt
# Extract point numbers, save to text file
less lastpage.pdf | cat | grep points: | sed -r 's/^.*points://' | sed -r 's/\s*//' | sed -r 's/,/\./' | sed -r 's/\n/\,/' >> "$filerootname"_a.txt
# Extract point numbers, save to text file
less lastpage.pdf | cat | grep discretion | sed -r 's/^.*discretion\)://' | sed -r 's/\s*//' | sed -r 's/,/\./' | sed -r 's/-//' >> "$filerootname"_b.txt
#| sed -r 's/n\/a|na|no|No|NO|O|ok|OK|Ok//'
# Extract comments, append to text file
less lastpage.pdf | cat | sed -e "1,/Comments (Optional)/d" | sed '/^\s*$/d' | sed -r 's/\x0c//' | sed 's/^.\{1,\}$/"&"/' >> "$filerootname"_c.txt
# Delete new lines in comments
#tr "\n" " " < "$filerootname"_c.txt | sed 's/,$/,\n/' > "$filerootname"_d.txt
# Put each line in quotes
#awk '{ print "\""$0"\""}' "$filerootname"_d.txt
cat "$filerootname"_a.txt >> $filerootname.txt
cat "$filerootname"_b.txt >> $filerootname.txt
cat "$filerootname"_c.txt >> $filerootname.txt
# Replace new lines with commas in text file
tr "\n" "," < $filerootname.txt | sed 's/,$/,\n/' > $filerootname.csv
cat $filerootname.csv
cat $filerootname.csv >> output.csv
# Measure file size of $filerootname (stored in a variable)
filesize=$(wc -c <$filerootname.txt)
# Check if $filerootname.txt has 17 bytes or less (six empty lines+code number)
if [ $filesize -le 17 ]
then
echo "failed to extract information from $filerootname.pdf"
mv -v lastpage.pdf failed/$filerootname.pdf
mv -v $filerootname.txt failed/$filerootname.txt
fi
# Delete the PDF
rm -v $filerootname.csv
rm -v $filerootname.txt
rm -v "$filerootname"_*
rm -v $filerootname.pdf
rm -v lastpage.pdf
done
echo "#"
echo "#"
echo "#"
echo "done extracting the PDFs"
rm -rf return
cd ../
echo "end of script."
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