Please help with script...

Robert Oelschlaeger roelsch at acm.org
Tue Jun 4 01:53:24 EDT 2002


"Topy" <hobiewon at hotmail.com> wrote in message
news:adheqc$nhn$1 at netnews.upenn.edu...
> Hey all,
> I'm having trouble with a problem I have with this python program.  It's
> kind of long, so if anyone has the patience, I would gladly appreciate
your
> time(the programming is fairly simple I think.)  Anyway, the problem I
need
> to do is this :
<SNIP>

> It's not much, but I'm not sure where to go from here...can anyone help?
>
> Thanks so much!
> Topy

This might get you started; the worksheet is left open for you to examine
and save via the File menu -- Bob

#!/cygdrive/c/Python22/./python
# vim:ts=4:sw=4:et

def main():
    ProductID = int(raw_input("Please enter the ProductID: "))
    OrderQty = int(raw_input("Please enter the cutoff order quantity: "))
#   CSV = open('a:/sales.txt', 'r')
    CSV = open('sales.txt', 'r')

    import win32com.client
    xl = win32com.client.Dispatch('Excel.Application')
    xl.Visible=1
    xl.Workbooks.Add()

    # start at the top of the worksheet
    xl.Range("A1").Select()

    # report selection parameters and skip a line
    xl.Range("A1:B1").Value = ("ProductID:", ProductID)
    xl.Range("A2:B2").Value = ("OrderQty:", OrderQty)
    xl.ActiveCell.Offset(4).Activate()

    # fill in the column headers
    xl.ActiveCell.Range("A1:E1").Value = ("OrderID", "OrderDate",
"RegionID", "ProductID", "OrderQty")
    xl.ActiveCell.Offset(2).Activate()

    # process each line in the CSV file
    for line in CSV.readlines():
        # remove trailing \n
        line = line.rstrip()

        # get the raw data as strings
        inOrderID, inOrderDate, inRegionId, inProductId, inOrderQty =
line.split(",")

        # is this item one that is interesting?
        if int(inProductId) == ProductID:
            if int(inOrderQty) >= OrderQty:
                # write the input data to the output file
                xl.ActiveCell.Range("A1:E1").Value = (inOrderID,
inOrderDate, inRegionId, inProductId, inOrderQty)
                # and activate the next line
                xl.ActiveCell.Offset(2).Activate()


if __name__ == "__main__":
    main()







More information about the Python-list mailing list