hey cameron mckenzie here i'm the editor-in-chief over at the serverside.com and you can follow me
on twitter at cameronmcnz right now i want to talk to you about extracting data from multiple
pdf files i've got a bunch of tutorials that have taken you to this point
showing you how to do it one file at a time but here we pull it all together and
start extracting some data from a bunch of different pdf files all in the same folder
so a good way to start this off is creating a new process project and i'll call this extract
from multiple pdfs because that's exactly what i want to do here now as that starts up i'm going to
show you these different pdf files that i've got i've got three on my file system in a folder named
orders i believe yep there it is right there and as you can see these uh these different forms are
very similar right they're similar in structure but they've all got different line items they've
all got different totals and different amounts what i'd like to do is i'd like to go through
all three of these and as i'm going through all three of these i want to extract the total
and i want to add that total together so that when i'm done i've got a tabulation of the total
from all of these invoices i'm not going to do structured data that is tabular type of data
that's in here and in here i'll do that in the next tutorial here i'm going to be looking at
data specifically that's sort of in that name value pair type of syntax that you extract when
you use the anchor activity and i'm only going to do one property on each of these fields i mean i
could do all of them but it would take an hour so brevity is the soul of wit as they say so let's
kick this off the first thing i need is a for each loop inside of my main window and that means
clicking on that open main window going down to workflow looking at control
and then finding the for each loop and dragging it on and what i want to do here is i want to go
through all of the files in that folder and so you just say driver directory
dot get files and then in quotes the name of the folder which i believe is orders
let me double check yeah there it is order so it's going to be there i'll throw these into
the root of my project so that anybody that wants to do this particular example on their
own can can get these files all off of github was the project name extract from multiple pdfs
there it's in there just in case anybody wants those that's not necessary to get things going
here though okay so i've got those that'll load all of my files now what i want to do i want to
actually kick off an adobe process for each one of these files so that they will open up
and i can scrape the code in order to do that i have to start a process so i look for the start
process activity drag it here and of course it's these files that i'm going through so i just sort
of say file.tostring don't forget the tostring i always do and that'll allow each file at a time
to kick off a new process now the process i want to kick off is attaching the adobe window process
to these pdf files so i have to go attach window and then that attach window activity it wants you
to point to the adobe acrobat reader fox it or whatever you're using so you'll notice that right
here it says okay you want to attach a window to this file what window do you want to interact
with so you have to click on indicate window screen and then make sure that your pdf viewer
is open oh jeez that's not my pdf viewer that's microsoft word there we go there's the pdf viewer
okay looking handsome so i'll say indicate window on screen
click that and life is good now things will start up in this window one thing to note when you click
on the edit selector you'll notice that it's hard-coded a1a001 into the selector you want to
deselect that that's poison if you leave that in there and you try and loop through multiple files
and again you just click on that hamburger there edit selector deselect the title but
if you go through this and if you try and go through multiple files and it keeps trying to
look for the same pdf file you're going to get blocked it's going to have a problem so you got
to get rid of that that's the key to being able to loop through multiple items okay but once we've
done that well we could start actually going in and grabbing text the text that we want is this
here it's the total that total number the number is going to change from one pdf to another so we
can't directly reference that value but we can reference this total field because every single
one of these pdf files says that word total down there in the same spot and so we're going to tell
the uipath hey open these files find that word total and then get the number underneath that
total each time i'm going to do that we add an anchor element on here so browse for anchor
anchor base right there and that works with the find element so we want to find that total so find
element we'll say what element do you want to find and again you got to move your windows
around so that you've got the pdf on the screen as well i know it's getting a little busy here
indicate element for the find element and it's that total field that we want here this will make
it possible to identify that field on any screen you see it likes that says okay down with the
total and then the next step is to call get text we want to get the text that's underneath that
and so we find get text drag that on here now we have to go through that similar process of just
pointing to the location of the text it actually doesn't really mean too much here but if we don't
do it we'll get an error so there we go that now points there comes up now it seems to understand
everything that we're trying to do now by the way you can change these titles here i'm not
going to because i just think it's one more step that's just going to prolong this tutorial i know
people want to get to the important stuff but you can change that if you like so this is going
to pull out the value underneath the word total now the anchor base has this automatic setting
where it's supposed to work automatically and figure out what you what you want it to do
i'm not convinced that it's going to work here so one of the things you can specify is where your
anchor is right now the anchor total is on top of the number and so i'm just changing that at the
top again i clicked on anchor base not the element but anchor base and specify the anchor position
is top above the number that we're going after and again that's going to be the number on each of
those different pdf files and so that's going to get the number i want to hold it in a variable and
i actually want two variables here because i want to keep i need the invoice total but i also want
to keep track of a total a sales total between all of the different all the different invoices so i'm
going to add two variables here the first variable is going to be named invoice total so type string
and i'll give it scope to the entire sequence and the second one is going to be called total sales
and the total sales watch this browse for types it's trouble it's double trouble right there
micro core system double and i'll make that a sequence scope as well
now here's the deal every time we pull data out of out of an invoice we assign the invoice total
okay so that is now going to assign the invoice total but every time we get a new
pdf file we want to update the total sales and so that means adding in an assignment
and so right after we get the number on the invoice i want
to do this little razzle dazzle here i want to say the total sales is equal to
the total sales plus
the new invoice total now i got to do something crazy here i got to say convert dot 2 double
invoice total the reason i have to do that is because invoice total is a string and the
total sales is a double and that won't convert explicitly so you have to actually put a little
bit of a method in there to make that conversion happen but if you do that
everything will come up roses now after i've done that i'm going to
just do a little bit of an output so i'm going to write to the console
do the little right line activity and say the running total is plus total sales dot tostring
this is a double you got to turn it into a string and so there you go i think i got some of the
stuff that i used here that's the i think the orders folder the ongoing total and that's the
convert to double invoice total text that i had to put in there to make that work so i'm just leave
that there you can pause the screen if you want to see that for a second but that gives you an idea
of what i've done there now i'm going to look make sure that i don't have any errors anywhere
i don't see any exclamation points that all looks good i always forget to do two string in there and
sometimes i forget to assign the variable here to invoice total but that looks like it's been done
oh and one thing i forgot to do was close the application so i'm going to take a little close
application activity here i was throwing some things off so i need to close the application
right after i get that text it wants me to indicate which application i'm going to close
so i'm going to come over here and indicate that it's this pdf viewer that i want to close
i'm going to click control s to save and then run the file
and here you can see it closes the file goes to b2b closes the file goes to 3c3
and then finally when it comes up you see that it's now gone through and it's kept that running
total and the running total starts at 115 then it goes to 460 and then it goes to 690.
and there you go that's how you can loop through all of the different invoices in a particular
folder extract some data from the pdf and then maybe do something meaningful with that data such
as running an ongoing total of how many sales you're getting from your invoices
and there you go that's the ins and outs of extracting data from multiple pdf files
that are sitting there in the same folder anyways head over to theserverside.com we've got lots of
other great tutorials on uipath and anything to do with enterprise software development
if you want to follow my personal antics i'm on twitter at cameronmcnz and subscribe on youtube