I'm files name list in directory to excel report via python.My code can run but when i convert it to function it error = "list index out of range" This's my code before convert to function:
xfile = openpyxl.load_workbook('C:/Users/11359023/Desktop/movemail_project/report_ie.xlsx')
sheet = xfile.get_sheet_by_name(cut_name_sheet("D:/UAT Move mail/01-FIN & ACC-Confidential/Inbound/IE-Direct/"))
#Header
sheet['A2'].value = "Property Files Report as at " + str(d1)
sheet['A4'].value = "Files"
sheet['B4'].value = "Source path"
sheet['C4'].value = "Destination path"
x=5
col = "A"
row = x
y=5
col2 = "B"
row2 = y
for root, dirs, files in os.walk("D:/UAT Move mail/01-FIN & ACC-Confidential/Inbound/IE-Direct/"):
for file in files:
if file.lower().endswith('.pdf'):
sheet['{0}{1}'.format(col, row)].value = file
row += 1
sheet['{0}{1}'.format(col2, row2)].value = root+"/"+str(file)
row2 += 1
xfile.save('report_ie_x.xlsx')
This's code when I convert to function:
def report_copyie(z):
xfile = openpyxl.load_workbook('C:/Users/11359023/Desktop/movemail_project/report_ie.xlsx')
sheet = xfile.get_sheet_by_name(cut_name_sheet(z))
#Header
sheet['A2'].value = "Property Files Report as at " + str(d1)
sheet['A4'].value = "Files"
sheet['B4'].value = "Source path"
sheet['C4'].value = "Destination path"
x=5
col = "A"
row = x
y=5
col2 = "B"
row2 = y
for root, dirs, files in os.walk(z):
for file in files:
if file.lower().endswith('.pdf'):
sheet['{0}{1}'.format(col, row)].value = file
row += 1
sheet['{0}{1}'.format(col2, row2)].value = root+"/"+str(file)
row2 += 1
xfile.save('report_ie_x.xlsx')
Please tell me what is the problem?