MP3 Tags to Excel Not working what can I do?

Hi,

I am trying to finish this function so that it cycles through all the songs in a folder and put the MP3 Tags into excel. How do I do the code below so it cycles through the MP3s Tags and add them into the excel document instead of hard coding them? Thanks

def ExtractMP3TagtoExcel():
        
    print('Start')
    
    tracks= []
    gettags =[]
    for root, dirs, files, in os.walk('C:\\Users\\mrdrj\\Desktop\\sdf\\'):
        for name in files:
            if name.endswith(('.mp3','.m4a','.flac','.alac')):
                
                tracks.append(name)
                
                try:
                    temp_track = TinyTag.get(root + '\\' + name)
                    gettags =[temp_track.album, temp_track.albumartist,temp_track.artist,temp_track.audio_offset,temp_track.bitrate ,temp_track.comment ,temp_track.composer,temp_track.disc ,
                              temp_track.disc_total, temp_track.duration,temp_track.filesize,temp_track.genre, temp_track.samplerate,temp_track.title,temp_track.track,temp_track.track_total, temp_track.year]
                    print(root, '-',temp_track.artist, '-', temp_track.title)                   
                except TinyTagException:
                    print('Error')
    print(list)
    print(gettags)
    print('This is the name')
   
    wb = Workbook()
    os.chdir('C:\\Users\\mrdrj\\Desktop\\cqq\\Brimstone CD 3\\')
    dest_filename = '11empty_book1.xlsx'

    ws1 = wb.active
    ws1.title = "MP3 Info" # Main Tab
    
    for col in range(1, 2): # How many Tabs
        #ws1.append(range(5)) #Row Add values
        for row in range(2, number_files): 
            for col in range(1, 8): # Number of colums static
                _ = ws1.cell(column=col, row=row, value ='Test') #value="{0}".format(get_column_letter(col)))

                ws1['A1'] = 'Album'
                ws1['A2'] = temp_track.album
                ws1['B1'] = 'Contributing Artists' 
                ws1['B2'] = temp_track.artist
                ws1['C1'] = 'Title'        
                ws1['C2'] = temp_track.title
                ws1['D1'] = 'Total Number of Disk'
                ws1['D2']=  temp_track.disc_total
                ws1['E1'] = 'Genre'
                ws1['E2'] = temp_track.genre
                ws1['F1'] = 'Disc Number'
                ws1['F2'] = temp_track.disc
                ws1['G1'] = 'Track Duration'
                ws1['G2'] = temp_track.duration/60
                ws1['I2'] = col

                #print(temp_track)      
                #ws1['H1'] = 'Name'
                #ws1['H2'] = 
                #print('This track is by %s.' % temp_track.artist)
                #print('It is %f seconds long.' % temp_track.duration)
                #ws1['D2'] = temp_track.name
                #print(temp_track.track_total)
                #ws2 = wb.create_sheet(title="P") #Second Tab

        row = 2
        col = 2
              #ws2['F5'] = 3.14 # Add value to specific Tab

                    
ExtractMP3TagtoExcel()

@mrdrjay, I believe you would loop though all the MP3 tags and save the output in a list.

I modified the the gettags to gettags.append which captures all. Thanks. My hiccup now is to arrange the below to have Title, Artist, etc in the columns and the other values added to the rows.

for col in range(1, 2): # How many Tabs
     #ws1.append(range(5)) #Row Add values
        for row in range(2, number_files): 
            for col in range(1, 8): # Number of colums static
                _ = ws1.cell(column=col, row=row, value ='Testw') #value="{0}".format(get_column_letter(col)))

                ws1['A1'] = 'Album'
                ws1['A2'] = temp_track.album
                ws1['B1'] = 'Contributing Artists' 
                ws1['B2'] = temp_track.artist
                ws1['C1'] = 'Title'        
                ws1['C2'] = temp_track.title
                ws1['D1'] = 'Total Number of Disk'
                ws1['D2']=  temp_track.disc_total
                ws1['E1'] = 'Genre'
                ws1['E2'] = temp_track.genre
                ws1['F1'] = 'Disc Number'
                ws1['F2'] = temp_track.disc
                ws1['G1'] = 'Track Duration'
                ws1['G2'] = temp_track.duration/60
                ws1['I2'] = col
gettags.append([temp_track.album, temp_track.albumartist,temp_track.artist,temp_track.audio_offset,temp_track.bitrate ,temp_track.comment ,temp_track.composer,temp_track.disc ,
                              temp_track.disc_total, temp_track.duration,temp_track.filesize,temp_track.genre, temp_track.samplerate,temp_track.title,temp_track.track,temp_track.track_total, temp_track.year])

print(gettags)

The code above will create one large list inside of a list.

Thanks I have updated the code with the following :

for col in range(1, 2): # How many Tabs
     #ws1.append(range(5)) #Row Add values
        for row in range(1, number_files): 
            for col in range(1, 8): # Number of colums static
                print('------View Results------')
                for r in gettags2:
                    column_cell = 'A'
                    ws1['A1'] = 'Album'
                    ws1[column_cell + str(row + 1)] = gettags2[0]
                    column_cell = 'B'
                    ws1['B1'] = 'Contributing Artists'
                    ws1[column_cell + str(row + 1)] =  gettags2[1]    

                    column_cell = 'C'
                    ws1['C1'] = 'Title'
                    ws1[column_cell + str(row + 1)] = gettags2[2] 

                    column_cell = 'D'
                    ws1['D1'] = 'Total Number of Disk'
                    ws1[column_cell + str(row + 1)] = gettags2[3] 

                    column_cell = 'E'
                    ws1['E1'] = 'Genre'
                    ws1[column_cell + str(row + 1)] =  gettags2[4] 

                    column_cell = 'F'     
                    ws1['F1'] = 'Disc Number'
                    ws1[column_cell + str(row + 1)] =  gettags2[0] 

                    column_cell = 'G'
                    ws1['G1'] = 'Track Duration'
                    ws1[column_cell + str(row + 1)] =  gettags2[0] 

                    print(r)

I tried to use gettags2[0], gettags2[1], etc but it assigns the last MP3 data to the columns A: 111111, Column B: 2222222, etc. How can I update the last for loop so it finishes the process. Thanks

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.