APPLICATION - Migrating Records from a Text File to an Excel Workbook

Python 3: Automating Your Job Tasks Superhero Level: Automate Excel Tasks with Python 3
18 minutes
Share the link to this page
You need to have access to the item to view this lesson.
One-time Fee
List Price:  $139.99
You save:  $40
List Price:  €128.82
You save:  €36.80
List Price:  £109.74
You save:  £31.35
List Price:  CA$191.13
You save:  CA$54.61
List Price:  A$210.68
You save:  A$60.19
List Price:  S$188.83
You save:  S$53.95
List Price:  HK$1,092.95
You save:  HK$312.29
CHF 91.34
List Price:  CHF 127.88
You save:  CHF 36.54
NOK kr1,052.31
List Price:  NOK kr1,473.28
You save:  NOK kr420.96
DKK kr686.58
List Price:  DKK kr961.24
You save:  DKK kr274.66
List Price:  NZ$227.97
You save:  NZ$65.13
List Price:  د.إ514.17
You save:  د.إ146.91
List Price:  ৳16,408.23
You save:  ৳4,688.40
List Price:  ₹11,637.16
You save:  ₹3,325.14
List Price:  RM657.67
You save:  RM187.92
List Price:  ₦206,269.66
You save:  ₦58,938.40
List Price:  ₨38,916.02
You save:  ₨11,119.65
List Price:  ฿5,127.43
You save:  ฿1,465.08
List Price:  ₺4,505.30
You save:  ₺1,287.32
List Price:  B$723.91
You save:  B$206.84
List Price:  R2,571.91
You save:  R734.88
List Price:  Лв252.38
You save:  Лв72.11
List Price:  ₩190,499.14
You save:  ₩54,432.21
List Price:  ₪515.38
You save:  ₪147.26
List Price:  ₱8,140.48
You save:  ₱2,326.02
List Price:  ¥21,963.82
You save:  ¥6,275.82
List Price:  MX$2,336.09
You save:  MX$667.50
List Price:  QR510.10
You save:  QR145.75
List Price:  P1,900.64
You save:  P543.07
List Price:  KSh18,618.67
You save:  KSh5,320
List Price:  E£6,599.78
You save:  E£1,885.78
List Price:  ብር8,037.80
You save:  ብር2,296.67
List Price:  Kz118,908.34
You save:  Kz33,976.24
List Price:  CLP$126,117.11
You save:  CLP$36,036.03
List Price:  CN¥1,014.10
You save:  CN¥289.76
List Price:  RD$8,237.97
You save:  RD$2,353.87
List Price:  DA18,843.53
You save:  DA5,384.25
List Price:  FJ$311.92
You save:  FJ$89.12
List Price:  Q1,086.64
You save:  Q310.49
List Price:  GY$29,266.81
You save:  GY$8,362.54
ISK kr13,793.62
List Price:  ISK kr19,311.62
You save:  ISK kr5,518
List Price:  DH1,395.33
You save:  DH398.69
List Price:  L2,480.56
You save:  L708.78
List Price:  ден7,930.17
You save:  ден2,265.92
List Price:  MOP$1,125.70
You save:  MOP$321.65
List Price:  N$2,570.90
You save:  N$734.59
List Price:  C$5,148.73
You save:  C$1,471.17
List Price:  रु18,603.56
You save:  रु5,315.68
List Price:  S/522.95
You save:  S/149.42
List Price:  K543.64
You save:  K155.33
List Price:  SAR525.05
You save:  SAR150.02
List Price:  ZK3,733.66
You save:  ZK1,066.83
List Price:  L641.14
You save:  L183.19
List Price:  Kč3,187.01
You save:  Kč910.64
List Price:  Ft49,549.41
You save:  Ft14,157.98
SEK kr1,062.40
List Price:  SEK kr1,487.41
You save:  SEK kr425
List Price:  ARS$124,696.09
You save:  ARS$35,630
List Price:  Bs966.60
You save:  Bs276.19
List Price:  COP$540,370.17
You save:  COP$154,402.50
List Price:  ₡71,720.93
You save:  ₡20,493.15
List Price:  L3,457
You save:  L987.78
List Price:  ₲1,052,195.01
You save:  ₲300,648.62
List Price:  $U5,389.29
You save:  $U1,539.90
List Price:  zł547.82
You save:  zł156.53
Already have an account? Log In


Hi again and welcome to this lecture. Throughout this lecture, we are going to build an application that will migrate all the data inside a text file as you can see right here to an Excel spreadsheet. Basically, the application will create the sheet itself the file, the table within the sheet, the columns and rows will split this data inside the text file using the semi colon as a delimiter. Then the application will apply some styles some visual styles on our sheet on our table. And we will have column headers, filters, various colors, row stripes and column stripes as well for better readability of the table. And at the end, the application will identify all the salaries inside the table that are greater than $55,000.

And for those particular cells, it will make the text within the cells red instead of the default black Also bold and italic, so we can have a better view over the employees that have the greatest salaries. Okay, so we will consider this text file right here, which has seven columns ID, first name, last name, department, phone address and salary for each employee. And then the values for each of these columns are separated by a semi colon that as I said, we will use as a delimiter when splitting each row of this file, okay, for now, keep in mind that immediately after this video ends in the next lecture, you will be able to download both the application code itself and this file so you can work on it and test it as well. So let's start working with this file. And let's start the migration process to an Excel workbook.

In order to do that, I'm going to open up the Python interpreter. And first of all, as always, we are going to import the necessary modules. So before anything else we have from open pi Excel, we are going to import a class name workbook next also from open pi Excel dot styles, import all in order to have available all the necessary styles that we will apply on each cell that meets the condition of having a value of over $55,000. Okay, and finally, we also need to other classes. So from open pi Excel dot worksheet dot table, we are going to import table and table style info. Okay, the next step is to open the text file that we've just seen for reading.

So I'm going to create a variable. txt underscore file equals open and in between the parentheses of open and also in between double quotes. I'm going to enter the full path to our file. So the backslash Excel app, backslash employees dot txt, this is a text file you've just seen. Okay, next we're going to create an empty list for storing the records as a list of lists. So we are going to store all the data inside the text file in the form of a list of lists.

So let me create an empty list records, let's say equals open close square brackets. And next we should make sure that we are reading the file from the very beginning. So txt file dot seek of zero as we already previously did inside this course. Okay, now it's time to have a look once again at our file. And as I said, we are going to split each line in the file by the semi colon delimiter. And then we will append each list generated by the read lines method to the new list called records.

So in order to do that, I'm going to write a for loop. So for record in text file, dot read lines, colon records, the name of the list dot append, we are using the append method and in between its parentheses. We are going to Enter record.or strip of backslash n. So we are going to strip away any newline characters at the end of each line in the file, and also dot split. And as I said, we're going to use the semi colon as a delimiter. Okay, let's hit Enter. And now let's check our list records.

And indeed, you can see that we now have a list of lists, where each element of the outer list is a list made up of the elements that were previously separated by a semi colon. Okay, so at this point, we've read the data from the file, we have populated this list called records. Now it's time to create our new workbook. Since as you can see right here in the Excel app folder that I have created, there is currently no Excel file in this folder. Okay, so I'm going to create the workbook right now. So workbook equals and now we are going to call the workbook class work book.

Now Next we are going to set the path or location where we want the new Excel file to be saved. So we are going to create a variable called file path equals. And now I'm going to paste in the path to the file, or actually the location where we are going to store this file. So the Excel app, my company stuff dot XLS x, this is the name of the file that we're going to create. Also note that we should escape the backslash characters inside the path using another backslash, so we would have a double backslash in order to avoid any conflicts with pythons special characters, like a backslash n, or a backslash T. Okay, so we have defined the file path the location of the file, the only thing we have to do in order to have our file created is workbook dot save of file path.

So we are going to create and save the file to the specified location. Now let's check the folder once again. And you can see that the Excel file has been created. So great job thus far. At this point, we have a new workbook on our file system. And each new workbook created by the open pi Excel module has by default, a single sheet called simply sheet.

In order to verify this, I'm going to use workbook dot sheet names. And as you can see, we have a list with a single element which is sheet. Okay, now let's assume that we want to rename the default sheet to let's say employees. In order to rename this default sheet, first, we have to reference it. So sheet equals workbook. And in between square brackets, we have the name of the default sheet which is sheet.

And now we can just do sheet dot title. And now we can enter the new name of the sheet, let's say employees. Okay, great. Now it's time to populate the sheet with the data we currently have inside the records list. Also note that the first Throw inside the text file. So this one right here will be considered the table header inside the Excel spreadsheet.

And that is just what we want because we want to have a properly formatted table with a header and then the rows containing the data. Okay, so now we can use this list of lists in order to populate our sheet. And we are going to do that using of course a for loop in order to iterate over the entire list. So for row in records, basically for each list inside the list of lists, colon, sheet, dot append of row, Okay, at this point, we should have the data migrated to the file. However, we want to use this data in order to create a table and then format it using different styles. So for that, we should create a table inside our sheet.

In order to create the table inside the sheet. We can just do table equals and now we use the table class. We import it previously. Inside the parentheses of this class, we enter two parameters, display name, which is a mandatory parameter equals table, let's say. And this is the most important parameter right here, ref equals. And now we will define the array inside the sheet that will actually contain the data itself.

Now looking at our text file, again, we know that we have seven columns, and also 11 rows. So that would be columns A to G, and rows one to 11. In order to define this array inside our code, we will use cell A one up to cell g 11. And that will basically be our table. Okay. Of course, if you would have had 100 rows inside your text file, then this array would have been a one to G 100.

But in our case, this is the array. So let's hit enter. The next thing we should to do is define a style for the table, meaning we can pick one of the default styles and specify the default style name. And also, we can specify if we want row and column stripes within our table in order to have everything look neat and pretty. There are multiple default styles for creating a table using the open pi Excel module. And in order to see all the styles, you can check out open bi Excel dot worksheet, dot table dot table styles.

Okay, we got an error because we haven't imported the entire open pi Excel module. And I'm going to do that right now. Since we are in a testing environment anyway, so let me try this again. Okay, and these are all the default table sheets inside the open Excel module. Of course, you can test which style suits your taste and your needs for your table. I'm going to choose one of these default styles, namely, table style, medium nine Because I like that one.

So let me show you how to define the style for the table. We have style equals, and now we use table style info that we previously imported. And in between these parentheses, first of all, we should specify the name of the default style that we want to use. So name equals, as I said, table style, medium, nine, comma, and we can also add additional options. For example, show row stripes equals true and also show column stripes equals true as well, in order to have a pretty and appealing table as a result. Okay, so this is the style we defined.

Now it's time to apply the style to our table. And for that, we are going to use table dot table style info equals style that we just defined. Okay. And of course, we should add the newly created table to our Because thus far, we have defined the table, we have defined the style for the table. And we have also applied the style to the table. But at this point, the table hasn't yet been added to the sheet.

So in order to do that sheet dot Add table, and in between the parentheses of our table with specify table, which is actually this instance right here. Okay, finally, we assumed that we want the salaries that are greater than 55,000 to be shown in red font instead of the default black font, and also as being bold and italic so we can see them better inside the table. For that, first we should define the font and also specify the bold and italic options. In order to do that, let's create font equals font color this attribute equals colors dot red, because I said that we want to have a red font for this particular cells, comma bald eagle Through comma, italic also equals true. And we want these font settings applied to the cells that meet the condition salary greater than 55,000. Now looking at our data again, you can notice that rows two to 11 are the rows containing the actual employee data.

And the salary will be the seventh column in this table, which is column G. Now since we want to take into consideration rows two to 11. Inside this table, we can use a trick by using the range function to iterate over the cells inside the table inside column G, which is associated with the salary. So in order to do that, I'm going to use a for loop for let's say cell number in a range of two up to but not including 12. So that would be 211. As I said, colon, now we should specify the condition if and now we are going to use The int function because we will have a string inside each of the cells on column G. And in order to evaluate if the value inside each cell is greater than 55,000 or not, we should have an integer. So int, and in between the parentheses of int, do you remember how we would reference a cell inside the sheet, it would be sheet of let's say in between square brackets, we would have b 10.

For example, well, in our case, since we are iterating, over a range, and we want to consider all the cells inside column G, we will have G, the column identifier. And now we will use a string format operator percent s, then we will use the percent operator and in between parentheses, we will use cell underscore number. So first of all, we will evaluate cell number g two, according to this range right here. That would be G and two maps to the value inside the parentheses. Then for the next iteration, we would have three extracted from this range, so we would have G three, and so on until this range is exhausted. Okay, so we have sheet of G to, for example, dot value, because we want to refer to the value inside that cell.

So if this value is greater than 55,000 colon, then we want to apply this font right here to each particular cell that meets the condition. So in order to apply a font to a cell, we would usually have a sheet of let's say, g 10 dot font equals this name right here, font. But since we have an iteration, and we are iterating, over all the rows, meaning to 11, then we will use the percent operator yet again. So percent s, and again, percent and cell number dot font equals font. So again, we are using this range right here two to 12, which actually means to up to and including 11, then we are evaluating the values inside each of these cells. So if the value is greater than 55,000, then we are applying the font that we have just defined up here to each of those cells meeting the condition.

Okay, let's hit Enter. And now the last thing we should do is, of course, save the workbook. So workbook dot save, file underscore path. Okay, of course, you should remember to also close the file that we used for reading the data, the txt file, so txt, file, dot close. And now let's go to the File and see if all of our changes all of our settings have been applied to the file. So first of all, you can notice that the file has been created my company staff dot XLS x.

Now let's open the file and let me zoom in a bit. First of all, you can see that all the data inside the text file right here has been successfully migrated to this file to this sheet and to this table, the first row inside the text file has been converted to the header of this table. We also have filters available if you want to filter our data. Next, let's notice the style applied on this table. So we have the column header with the color different than the rest of the table. We also have row stripes, you can see that we have alternating colors for the rows inside this table.

And also we have columns stripes. You can also see that the design of each column alternates as well. Finally, inside the G column, the column containing the salaries, you can see that all the salaries that are greater than 55,000 are now written in red, and the text has also been converted to a bold and italic version of the initial text. Okay, now let's close the file. And I'm going to delete this file. And I'm going to show you the application containing all the code that we discussed inside this lecture.

So this is the actual Excel file, in which you will find the code that we just executed throughout this lecture. And as a side note, I have also added some comments in order to have everything clear. Of course, as always, I strongly recommend you use as many comments as necessary inside your code for later reference. Finally, in order to prove that this application works as a whole, I'm going to open the windows command line. And I'm going to run this application so Python and then the path to the file, Enter. And now if we check our folder, the file has been created.

Once again, opening the file, we see that we have the same result as we've seen before, so our application works correctly. Okay, having that said, I hope you enjoyed this lecture and migrating data from an ugly text file to a clean and pretty Excel workbook, and I hope you're going to find all the skills inside the section useful for your career. I'll see you soon.

Sign Up


Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.