1 year ago

#374340

test-img

medusa

Python Script to read text from one excel file and convert it to a number value and write to a different excel file

I haven't written python since college so I am very out of practice. I am trying to have the script read from the second column starting at row 2, which contains text and change it to a value (0-6) and write this new value to a new excel workbook in the same cell as the value read in. Please be nice, I know it is probably an awful script, this was just a first attempt by someone without a lot of python experience, even in college. Thank you!!

I have ran this script a few times, in the command line it will display the converted value as it is running and it will produce an excel wb named "cop_out", but the excel workbook is blank. So something in my code is missing or wrong that should write to the new excel workbook.

from openpyxl import Workbook, load_workbook


#read in excel sheet
def main():

    #input file
        wb = load_workbook(filename = "Cop_status.xlsx", read_only=True, keep_vba=False)
        tab = wb.active # Cop_statushange to 1 if it breaks with Zero

        #output file
        output_wb = Workbook()
        output_ws = output_wb.active
        rowcount = 2
        
        
        #Iterate through each row (read in status and convert to value)
        for i in range(2, 100):
            cop_status = tab.cell(row=i, column=1).value
            zero = "0"
            one = "1"
            two = "2"
            three = "3"
            four = "4"
            five = "5"
            six = "6"
            
            if cop_status is None:
                break               
            elif cop_status == "OFF":
                print(zero)
                output_ws.cell(row=rowcount, column=1).value == zero
                rowcount= rowcount + 1 
            elif cop_status == "ON":
                print(one)
                output_ws.cell(row=rowcount, column=1).value == one
                rowcount= rowcount + 1 
            elif cop_status == "ONOPTOUT":
                print(two)
                output_ws.cell(row=rowcount, column=1).value == two
                rowcount= rowcount + 1 
            elif cop_status == "ONOS":
                print(three)
                output_ws.cell(row=rowcount, column=1).value == three
                rowcount= rowcount + 1 
            elif cop_status == "ONREG":
                print(four)
                output_ws.cell(row=rowcount, column=1).value == four
                rowcount= rowcount + 1 
            elif cop_status == "ONRUC":
                print(five)
                output_ws.cell(row=rowcount, column=1).value == five
                rowcount= rowcount + 1 
            elif cop_status == "ONTEST":
                print(six)
                output_ws.cell(row=rowcount, column=1).value == six
                rowcount= rowcount + 1 
            elif cop_status == "OUT":
                print(zero)
                output_ws.cell(row=rowcount, column=1).value == zero
                rowcount= rowcount + 1 
        
        output_wb.save("cop_out.xlsx")
    
        
main()
            

python

excel

data-conversion

python-3.9

0 Answers

Your Answer

Accepted video resources