Category: Python
-
Introduction
- Today we are going to see how to read the excel’s each cell and pass the value to the REST API request
- On successful response we will parse the JSON response
- Read the JSON element and write it to the excel
Prerequisite
- In your terminal or CLI run
pip3 install package_name (Based on your OS instead of pip3 run pip. In some OS both python2 and python3 installed then check your pip version pip --version or pip3 --version and execute the command) - We need the following packages to be installed
requests and openpyxl
- Command:
pip3 install requests openpyxl
- Once installed please follow next heading to do the coding part
Coding
- The first three lines import the packages
requests on line 1 is used to do the REST API callopenpyxl on line 2 is used to do excel related thingstime on line 3 is used to delay the API call to a certain limit of time- In line 6 we assigned API URL to the variable
API_URL - From line 9 to line 23 the function
extract_country_details() declaration is present - Lets we see inner working of this function
- In line 9, we declared function with single parameter
search_term - In line between 11 and 14 we declared the dictionary variable with two element which will be pass as a query parameter for the API call
- In line 16, we declared a new variable
response and assigned it with the response we get from API call by using/importing the package import requests and call it’s method get() with two arguments - The first argument is the URL which we used to fetch the content
- The second argument is keyword argument which means we assign our dictionary variable’s value to the argument
params it will be query string for our API call - In line 18 convert/decode the received response to JSON object by calling
json() method - In line 20 and 21 we check response status is not equal to 200 if so then return the string
'error' - If everything goes well in line 23 we return the first index content of variable
json
- The second function from line 26 to 42 which is
write_country_details() is used to learn read and write excel using the package openpyxl let’s we see the inner workings- In line 27 we are assigning our excel filename
top5_populated_countries.xlsx to the variable filename. The excel file is in same path as our country.py - In line 29 we are calling the
openpyxl package’s method load_workbook(filename) with file path as parameter and assigning it to the variable wb (wb is short notation of workbook) - In line 30 for the variable
ws (worksheet) we assigning the workbook object’s member variable wb.active which holds the active sheet by default it will be the first worksheet - In line 31 for the variable
wc (workcolumn) we assigning the A column of the worksheet - In line 33 we assign value 1 to the variable
i - In line 34 to 40 the for loop is present we will see for loop in detail
- In line 34 we looping workbook’s column A if you closely noted we left the first row of workbook by calling
wc[1:] the [1:] denotes that start from 1st index to till the last index instead of 0th index - In line 35 during each looping we increment the variable
i - In line 36 we are calling the method
extract_country_details by passing A column’s cell value and assigning to the variable country_details - In line 37 we called
time.sleep(10) which is for pausing the script for 10 seconds so it won’t execute the next line till 10 seconds - The purpose of sleep timer is to avoid continuous hit to the API server because some servers may block the API call if we hitting continuously without a gap
- In line 38
we fetching associative/named index
capital from dictionary variable country_details and assigning it to the worksheet column B’s cell B2 in ws object - In line 39 & 40 the logic is similar to line 38
- The loop between line 34 and 40 will loop until the last value of A column which have list of country name
- Finally in line 42 we calling workbook’s method
save with filepath as argument which will write the fetched details such as each country’s capital, region and population in B, C & D column
- We explored two functions definition finally at the line 45 we calling our function
write_country_details()
Conclusion
- We learned how to call API service, read excel and write excel
- Please click here to download the excel file used in this script exercise