Error invoking python method new problem

Hello all,i invoke python method in Uipath,but some methods invoke right and some wrong,here are my uipath settings and python codes,if you know the problem pls help me ,thank you in advance!

#!/usr/bin/python3

-- coding: utf-8 --

from openpyxl import load_workbook
import win32com.client as win32
import numpy as np
import pandas as pd
import datetime
#getting specified format of excel

The method change_excel_name invokes right,operate_excel_bsh invokes wrong.

def change_excel_name():
file1 = “C:\Users\yx\Documents\SAP\SAP GUI\BSH.xls”
excel = win32.gencache.EnsureDispatch(‘Excel.Application’)
wb = excel.Workbooks.Open(file1)
wb.SaveAs(file1 + “x”, FileFormat=51)
wb.Close()
excel.Application.Quit()
return

def operate_excel_bsh():
file2 = load_workbook(“C:\Users\yx\Documents\SAP\SAP GUI\BSH.xlsx”)
ws = file2.active

ws.delete_rows(idx=1,amount=9)
ws.delete_rows(idx=2)
ws.delete_cols(idx=1)
ws.delete_cols(idx=3)
ws.insert_cols(idx=5)
ws.cell(1, 5, "New invoice")

x = 2
while x <= ws.max_row:
    ws.cell(x, 5, value=ws.cell(x, 6).value / ws.cell(x, 4).value)
    x += 1
file2.save("C:\\Users\\yx\Documents\\SAP\\SAP GUI\\BSH.xlsx")

file_wk = load_workbook("D:\\yxx\RPA\\Program\\FIN BSH auto invoice process\\BSH Invoice 2020(K).xlsx")
wk = file_wk['202005']
max_cell = 1
for row in wk.iter_rows(min_col=1, max_col=1,min_row=2, max_row=""):
    for cell in row:
        if cell.value != None:
            max_cell = cell.value
count = 0
for row in ws.iter_rows(min_col=1, max_col=1, min_row=2, max_row=""):
    for cell in row:
        if cell.value != max_cell:
            count += 1
        else:
            ws.delete_rows(2,count+1)
        break
    break

file2.save("C:\\Users\yx\Documents\SAP\SAP GUI\BSH.xlsx")

bsh = pd.DataFrame(pd.read_excel('C:\\Users\yx\Documents\SAP\SAP GUI\BSH.xlsx',sheet_name='BSH'))
bsh.sort_values(by='DocCa',ascending=False, inplace=True)
bsh.to_excel('C:\\Users\yx\Documents\SAP\SAP GUI\BSH.xlsx',sheet_name='BSH', index=False)

return

def replace_excel_bshk():
file_bsh = load_workbook(“C:\Users\yx\Documents\SAP\SAP GUI\BSH.xlsx”)
file_bshk = load_workbook(“D:\yxx\RPA\Program\FIN BSH auto invoice process\BSH Invoice 2020(K).xlsx”)
ws = file_bsh.active
now_time = str(datetime.datetime.now().strftime(’%Y%m’))
now_time1 = str(datetime.datetime.now().strftime(’%m/%d/%Y’))
ws1 = file_bshk[now_time]

i = 1
j = 0
for row in ws.iter_rows(min_row=2, max_row="", min_col=1, max_col=6):
    i += 1
    for cell in row:
        j += 1
        print(cell.value)
        ws1.cell(row=i,column=j,value=cell.value)
    j = 0
i = 1
for row in ws.iter_rows(min_row=2,max_row="",min_col=7,max_col=7):
    i += 1
    for cell in row:
        ws1.cell(row=i,column=11,value=cell.value)
        ws1.cell(row=i,column=13,value=now_time1)
file_bshk.save("D:\\yxx\RPA\\Program\\FIN BSH auto invoice process\\BSH Invoice 2020(K).xlsx")
return

@yu.xingxing

Did you try in python? Is it working well?

also you need to take care of indentation also

Hope this helps

Thanks

Yes,it works well in python.
But the method operate_excel_bsh invokes wrong in uipath.

@yu.xingxing

Can you share your workflow with files, so that we can try

Thanks

Invoke python method.zip (21.3 KB)
Pls have a look .

@yu.xingxing

I only receive the uipath project file, i didn’t recevie any Python File, also in the path i see there are spaces, you can try to avoid spaces in the path by using ‘_’

Also attached the python script and required sample files

Thanks

exceloperation.zip (1.1 KB)

Soory,here is my python code

@yu.xingxing

I can’t replicate the problem, because i don’t have any excel file, but in python scope, check the path is correct also check is python is x86 or x64

Thanks

Thank you anyway!

1 Like

Hi,I find a new information,
I create a new python Method,if i add “ws.delete_cols(idx=1,amount=9)”, the method invoke in uipath will be wrong,but if delete it ,then it’ll be right.Do you know why?

def test():
file2 = load_workbook(“C:\Users\yx\Documents\SAP\SAP GUI\BSH.xlsx”)
ws = file2.active
#ws.delete_cols(idx=1,amount=9)
file2.save(“C:\Users\yx\Documents\SAP\SAP GUI\BSH.xlsx”)
return