이번 시간에는 Find First/Last Data Row activity를 python 코드로 구현해보는 시간을 가져보자.
(uipath Find First/Last Data Row activity 알아보기)
def find_first_last_data_row(file_name: str, sheet_name: [str, int], skip: int, header: bool = True,
first_row_offset: int = 0, last_row_offset: int = 0):
wb = load_workbook(file_name)
ws = wb[sheet_name]
data = []
for row in ws.iter_rows(values_only=True):
if all(value is None for value in row):
skip -= 1
if skip == -1:
break
data.append(row)
if header:
df = pd.DataFrame(data[1:], columns=data[0])
else:
df = pd.DataFrame(data[1:], columns=None)
while True:
last_row_is_nan = df.iloc[-1].isna().all()
if last_row_is_nan:
df = df.drop(df.index[-1])
else:
break
first_row_index = df.iloc[0].name - first_row_offset + 2
last_row_index = df.iloc[-1].name - last_row_offset + 2
if last_row_index < -1:
last_row_index = -1
return first_row_index, last_row_index
def find_first_last_data_row(file_name: str, sheet_name: [str, int], skip: int, header: bool = True,
first_row_offset: int = 0, last_row_offset: int = 0):
wb = load_workbook(file_name)
ws = wb[sheet_name]
data = []
for row in ws.iter_rows(values_only=True):
if all(value is None for value in row):
skip -= 1
if skip == -1:
break
data.append(row)
if header:
df = pd.DataFrame(data[1:], columns=data[0])
else:
df = pd.DataFrame(data[1:], columns=None)
first_row_index = df.iloc[0].name - first_row_offset + 2
last_row_index = df.iloc[-1].name - last_row_offset + 2
if last_row_index < -1:
last_row_index = -1
return first_row_index, last_row_index