Home > Learning, Lesson, Problem, Projects/Experiments, Python > Python: Coffee Consumption P10

Python: Coffee Consumption P10


Learning : Python, SQlite3, Dataset, Pandas,
Subject: Create Coffee Consumption Application.

[NOTE: To keep the code as simple as we can, We WILL NOT ADD any user input Varevecations. Assuming that our user will Enter the right inputs.]

[ IF THE IS FIRST TIME DOWNLOADING THE CODE FILE, SELECT OPTION 7 FROM MAIN-MENU TO CREATE THE DATABASE]


In this Last part (Part-10) of Coffee Consumption App, we will write a Function to Export the Data to an Excel Fiel, So this will be the Last part of this application and any other Enhancement will be added as new version of the application.

Exporting The Data: To Export the data to or say “writing into” Excel file we need to import New library Named: xlsxwriter [if you don’t have the library then you need to install it first, using this command: pip install xlsxwriter]. After installing we will call it into our python file using this command: import xlsxwriter. The Excel file [in this case] will have four Sheets, named as follow:
coffee_con, coffee_name, coffee_type, coffee_size
each sheet will contains the table header and the date.
File Name: The file name will be as: coffee_consumption_[Current Date & Time].xlsx, the date and time will be formated as: yyyymmdd_HM, the python code to generate this is:
now = datetime.now()
date_t = now.strftime(“%Y%m%d_%H%M”)

Also we need to know that the file will be saved in the same directory of the application or (python .py file)


New Function: from our back_up function we will call another Function called: write_data(data_set,col_list,the_sheet): this Function will write the data-set into Excel file sheet, so we need to pass the data_set, column list and the Sheet Name as arguments to the function and it will use the command: write(row,col,data) to write the row to the sheet. Here is the code

# Writing the data into the sheet

def write_data(data_set,col_list,the_sheet):
    """
    Function to write the data-set into Excel file sheet.
    
    Argument:
        data_set:
        col_list: List of the columns in the data_set.
        the_sheet: the Sheet Name.      
    
    """    
    # write the Table Header
    for x in range (0,len(col_list)):
        the_sheet.write(0,x,col_list[x])
        
    rows = len(data_set)
    for row in range (1,rows) :
        for col in range (0,len(col_list)) :
            the_sheet.write(row,col,data_set[row][col])


In our main back-up function, we prepare the data-set and column list, then we call the new Function write_data passing the variable. Here is two parts of the code, first one is generating the file name, and calling the datetime function, second part is dealing with the data tables in the database, fetching the data, listing the columns and passing this to write_data function.. Here is the code

python project coffee ali doha radwani
python project coffee ali doha radwani

… Done. We finish the Coffee Consumption Application using Python and Sqlite. Next Week we will start New Application ..


..:: Have Fun with Coding ::.. 🙂


Part 1 Part 2 Part 3 Part 4 Part 5
Part 6 Part 7 Part 8 Part 9 Part 10


To Download my Python code (.py) files Click-Here



ali radwani ahradwani.com python projects codeFollow me on Twitter..

By: Ali Radwani

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: