In this particular lecture.
Let's go ahead and let's learn how we could read data from a file and sort of create a table out of
that particular file in a PDF format.
So the data which we will be reading over here is this data, which is nothing, but it's the data for
different countries which has the country, the capital, the area of the country in square kilometers
and the population of that country.
So all of this data which we have up over here, this data is actually present in a format which is
called as CSV, which also stands for comma separated values.
So as you can see, every value which we have over here is separated by a comma.
So we have a country, the value of the capital is separated by comma, and then the value of the area
is again separated by a comma, followed by the population.
So reading this kind of data in Python is quite easy, and that's what we will be doing here.
That is, we will be reading this data and we will be creating a table inside of the PDF file which
we have.
So in order to get this data on your machine, you could refer to the resources section of this particular
lecture and you will be able to get a file called as countries dot txt.
Okay, so once you have that file downloaded on your machine, make sure that you add that file to the
root directory of the current project which is PDF.
Okay.
So once that is added, let's head back to the main code file and we will be writing the code from scratch.
So the first and obvious thing which you need to import here is the Fpdf module.
So from fpdf I want to import Fpdf.
And after this, as we are reading the CSV data, I also have to import the CSV module as well.
So once these two modules are imported, I could now go ahead and open up the file.
So here I could say with open, open up the file which is countries dot txt.
I would also specify the encoding as UTF eight and I would open this thing up as a CSV file.
We are going to learn how to deal with CSV files later as well, but we are going to cover that in this
lecture as well.
Okay.
So once you have opened up the CSV file, the only thing which needs to be done is that you need to
read every single line from the file and you have to save it into a list.
Now each item in that particular list actually represents a single row.
So for example, when you go ahead and read the first line that represents one row, then this is the
second row, this is the third row, so on and so forth.
And finally you'll get a list of all the data which you have up over here.
So in order to do that, you have to use the CSV module.
So you have to say CSV dot use the reader method which allows you to read the file.
And to this you need to pass in the CSV file, which is the file header.
So I'll pass in CSV file.
So this kind of reads the CSV file by making use of the header.
And also after this you need to mention another parameter here which is delimiter.
Now delimiter is nothing but the separator which is used to separate out the values.
So in this case the delimiter is nothing, but it's a comma.
You could also have a delimiter like colons as well.
So if all the values which you had up over here, if they were separated by colons, you would have
to specify the delimiter as colon.
However, as this is a comma, I will specify comma here.
So this actually gives us back the data and this needs to be converted into a list.
So I would say list of this entire thing and let's save this list into something which is called as
data.
Okay?
So now this data actually contains all of the rows which we have inside the CSV file.
So now our job is to go ahead and put this data into a PDF file.
So let's create a PDF file.
So here we do not have any kind of a class which we will be inheriting from.
Instead, we will directly work with the PDF class itself.
So I would create an object of that.
So I would say PDF is going to be equal to PDF.
Once we have that particular object, I'll go ahead and straightaway set the font.
So pdf dot set font and I would pass in the font as.
Helvetica.
Set the size of this thing to, let's say, 14.
So now let's write the code for creating a basic table inside this.
So first of all, you have to create a page.
So PDF dot, add page.
This creates a page for us.
Now, in order to create a table out of this, I would go ahead and say with pdf dot table.
So we create a table and what we do is we access that table as table.
And once we have created this table, what we do is we get every single row from the above data which
we have.
So once we have the PDF table open up over here, now we are going to get every single row from the
above list which we have, and that list is data.
So what we do is we say for every single data row, because remember that this list which we have up
over here, this contains the items as this entire rows which we have saved as a single list item.
So we access every single data row in the data, which is our list.
Okay.
So once we got every single row inside the data, which is this data right here, which is the list
which contains all the rows, what we do is we create a table row for the PDF, which we have.
So the PDF table which we have up over here is called as table.
So in order to create a row inside the table, I would say table dot, row and let's save this thing
into a row.
So I could say row equals table dot row.
The next thing which needs to be done here is that we also need to loop through every single cell.
So after getting a single row, which is this one, you also need to get every single cell.
For example, you need to get the country, you need to get the capital, you need to get the area,
so on and so forth.
So in order to get every single data inside that data row, I could say for data or you could even say
for every single cell in the data row, what we now do is we create a cell for every single row which
we have.
So example, if we have first row and the first row, we have to create as many number of rows as many
entries this have.
So we have one, two, three and four entries.
So here, in order to create four cells, I would say take every single row and create a cell and then
pass in this datum over here.
Okay.
So now what we are essentially doing here is that in this line we are actually creating a PDF table.
And in this line we are essentially looping through every single item which is present in the list.
So this is one item, this is the next item.
And after getting to that, we are actually creating a row inside this table and we are not touching
this data as of now.
The next step is after creating the row, you also need to create every single cell inside this row.
So, for example, if you are reading one single row, you have to create one cell, second cell, third
cell and fourth cell.
So in order to create the cell, we are again looping through all the data which is present in the data
row, and we are calling it as data.
And then for every data point which we have, we are creating a cell inside that specific row.
So we have four entries.
So this creates four entries or four cells inside a single row.
And to this we are passing this data point, which is data which is nothing but a single unit of data
here.
And that's going to create a table for us.
Okay.
So once this thing is done, the final thing which needs to be done is that we need to output the file.
So I could say PDF dot that's going to be output.
Let's name this thing as table dot PDF.
So let's save this code.
Let's go back to the terminal here and if I run this code, we got an exception here in a line.
Okay.
So I've misspelled Helvetica.
So sorry for that.
I've missed a V here and once that is fixed I could run this code.
And now the table dot pdf file is created and if I open this thing up as you can see, this is the kind
of file we get.
So we have the country, capital area and population and all of the data is laid out in a proper fashion
inside this specific table.
So that means we have successfully converted the data which we had in this format.
That is a basic TXT file to a tabular format at pdf file up over here.
So this is specifically useful whenever you have to process raw data and convert it into a well formatted
data in a PDF format.
So in the next lecture let's learn how we could make this specific table a little bit better by adding
some kind of a styling to it.
So let's learn how exactly that could be done in the next one.