How to compare cell_values with xlrd and calculate the difference between them in Python 3.4
Question: It's kind of long story, sorry. So as ive been teaching myself python I've
been wondering how I could apply what im learning to my everyday life or work
so im not just doing random exercises and tutorials. I figured i could write a
script that could automate a portion of what i do at work.

Im a Food and Beverage manager for a hotel, so one thing i do is keep an
inventory of all the liquor, beer, and wine we have. I figured i could come up
with something that could:

  * Check my current inventory from an excel spread sheet

  * Compare it to what my par levels should be (quantities of liquor, beer, and wine that I should always have available at the hotel)

  * If my current inventory is less than what my par should be, the script will calculate the difference as well as the name of the liquor and export it to an email that would get sent out automatically.

  * It would do this twice a week Monday and Thursday at 9 am.

I'm not looking for solutions or an answer to this; this is something i want
to figure out on my own, but i've always had trouble imagining how to begin to
structure a script like this. I am not a beginner but im nowhere near
intermediate either. Should I use a bunch of functions, or maybe classes with
some methods? What would the "skeleton", i guess i could call it, of the
script be composed of?

Also, if someone could point me in the right direction to read up and learn
how to accomplish some of the things I want to do, like:

exporting data to an email and sending it out.

how to read individual cells in excel, and just working with python and excel
in general.

how to basically set the script up on a timer.

My computer is always on at work so i have no issue with this script
constantly running in the background.

I just started working on this today, i haven't had much time because of work.
Im a bit stuck on pulling info from my cell_value. Here is the code:

    
    
    import xlrd
    import xlwt
    import datetime
    
    file_location = "Your file path here"
    
    
    def par_num():
    
        par_workbook = xlrd.open_workbook(file_location)
        par_worksheet = par_workbook.sheet_by_name('Par')
    
        # Total number of rows with content in cells.
        num_rows = par_worksheet.nrows - 1
    
        num_cells = par_worksheet.ncols - 2
    
        # Current row for when iterating over spread sheet.
        cur_row = 2
        print(num_cells)
        # Iterates over work sheet
        while cur_row < num_rows:
            cur_row += 1
    
            cur_cell = -1
    
            print('--------------------')
    
            while cur_cell < num_cells:
                cur_cell += 1
                # Cell Types: 0 = Empty, 1 = Text, 2 = Number, 3 = Date, 4 = Boolean, 5 = Error, 6 = Blank
                cell_type = par_worksheet.cell_type(cur_row, cur_cell)
    
                # (Liq Name, Quantity in house)
                cell_value = par_worksheet.cell_value(cur_row, cur_cell)
    
                print(' ', cell_type, ' : ', cell_value)
    
    
    def inventory_num():
    
        inv_workbook = xlrd.open_workbook(file_location)
        inv_worksheet = inv_workbook.sheet_by_name('Sheet1')
        # Total number of rows with content in cells.
        num_rows = inv_worksheet.nrows - 1
    
        num_cells = inv_worksheet.ncols - 2
    
        # Current row for when iterating over spread sheet.
        cur_row = 2
    
        # Iterates over work sheet
        while cur_row < num_rows:
            cur_row += 1
            row = inv_worksheet.row(cur_row)
    
            cur_cell = -1
    
            print('--------------------')
    
            while cur_cell < num_cells:
                cur_cell += 1
                # Cell Types: 0 = Empty, 1 = Text, 2 = Number, 3 = Date, 4 = Boolean, 5 = Error, 6 = Blank
                cell_type = inv_worksheet.cell_type(cur_row, cur_cell)
    
                # (Liq Name, Quantity in house)
                cell_value = inv_worksheet.cell_value(cur_row, cur_cell)
    
                print(' ', cell_type, ' : ', cell_value)
    

ok, so I want to compare my cell_value, which comes up as (Ketel One, 5.6) for
example, against my par levels which would be like Ketel One: 8. But i only
want to compare the 5.6 to the 8, so I was guessing i would be able to put in
an " for/ if" statement within the while statement that would look something
like :

    
    
    for i in cell_value:
        if cell_value[1] < (float representing my par):
            print ((float representing my par) - i)
    

but i get back : "TypeError: unorderable types: str() < float()"

And when i try :

    
    
    print(' ', cell_type, ' : ', cell_value[1])
    

Just to see if i can pull up the quantity, it returns the following error:

    
    
    print(' ', cell_type, ' : ', cell_value[1])
    TypeError: 'float' object is not subscriptable
    

How are the contents of "cell_value" actually stored? Is it a tuple (x,y)? or
is it a list? why can't i just call the value with cell_value[1]?

Im kind of lost as to how i can actually compare the values on my spread sheet
against another value to calculate that difference in my "while" loop.

Also, please forgive me if my formatting is wonky, and I would appreciate any
and all criticism on my code layout or anything else.

Here is the link to my inventory sheet :

<https://docs.google.com/spreadsheets/d/1yXVFKiJMDJGPS6a6gaGn9qpIPNcgGetrLgfHGfdIm_U/edit?usp=sharing>


Answer:   1. The "`TypeError: unorderable types: str() < float()`"

This happens because you are evidently looking at a cell which has text
content, rather than a number content. Notice that in Excel (and hence xlrd) a
cell containing the text "123" is not the same thing than a cell containing
the number 123. So you have to ensure

  * that numbers in your inventory workbook are really recorded as numbers
  * that the script is not looking i.e. at a header thinking it contains a number

(for the records: python2 would not have raised this error... but things
wouldn't have been better, because the result of the comparison between a
float and a str would have been of no use to you)

In any case, to prevent the error you can just check beforehand that
`cell_type == 2`.

  2. The "`TypeError: 'float' object is not subscriptable`"

Notice this error should come out for a different cell than the previous - a
cell which does contain a number.

It simply depends from the fact that you are trying to access the value of the
cell as if it was a list (`cell_value[1]`), while it is not. The consequences
of this will depend on the content of such cell. If you try to access the item
in position 1 of the string "123", as in the previous example, you do get "2"
- and since this is not a number, but a string, you then get the error as in
the previous example. If instead you try to access the item in position 1 of
the float 123, you get an error even before, because you cannot "access an
item of a float".

