viewing paste Unknown #22591 | Text

Posted on the | Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#Import a cell from multiple excel worksheet files.
#12/17/2015
 
import os
import openpyxl
from openpyxl import Workbook
from openpyxl import load_workbook
 
index=1
 
wb = Workbook()
ws = wb.active
ws.title = "Tray List Import"
 
dest_filename = 'P:/Python Scripts/xlsx cell grabber/Traylist.xlsx'
 
path= 'T:/Docs/SITT/ElPasoSurgicalSpecialtyHospital/CountSheets/'
 
 
for root,dirs,files in os.walk(path):
    xlsfiles=[ _ for _ in files if _.endswith('.xlsx') ]
    for xlsfile in xlsfiles:
        ewb = openpyxl.load_workbook(os.path.join(root,xlsfile))
        n = len(wb.sheetnames)
        
 
        for s in range(n):
            #base = os.path.basename(os.path.join(root,xlsfile)) #for sheets with tray name in filename
            #trayname = os.path.splitext(base)[0] #Goes with the base variable, this will spit out the trayname from the file.
            sheet = ewb.worksheets[s]
            data1 = sheet['A1'].value
            ws['B' + str(index)] = data1
            ws['A' + str(index)] = xlsfile
            
            
        index=index+1
 
ws.column_dimensions['A'].width = 40
ws.column_dimensions['B'].width = 60
wb.save(dest_filename)
 
Viewed 693 times, submitted by Guest.