-3

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 my Excel report output.

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?

vee
  • 89
  • 2
  • 2
  • 8
  • You should work backwards from the traceback, which line has the bad index would be a good start? – Tonis F. Piip Jun 09 '20 at 08:41
  • Welcome to StackOverflow! Please read the [How To Ask](https://stackoverflow.com/help/how-to-ask) and provide a [Minimal Working Example](https://stackoverflow.com/help/minimal-reproducible-example), including the full traceback of the error you're getting – retnikt Jun 09 '20 at 08:42
  • Please provide the details of the error you are getting. The error message created by Python surely contains the line which it is related to. See also: http://idownvotedbecau.se/noexceptiondetails/ – Melebius Jun 09 '20 at 09:02

1 Answers1

0

Since we don't have stack trace in your question and full source i will be guessing

The error "list index out of range" is related to accessing list at wrong index - for details see this answer: Index Error: list index out of range (Python)

In your case you are accessing sheet as list and i believe that your sheet was not opened properly or something happened with that file while you rewrote your code into function. So make sure that your sheet (file) exists and you can access it using index

jana
  • 166
  • 1
  • 3
  • Thank you for pointing to the place where OP could find the answer useful for them. However, this is not enough for posting an answer. An answer on this site should _include_ a solution, not just a link to it, see [answer]. You can also [flag](https://stackoverflow.com/help/privileges/flag-posts) the question as a duplicate of another. – Melebius Jun 09 '20 at 08:59