Search code examples
pythonxlrdxlsxwriter

Python function to create a new excel workbook and populate cells


I want to write a Python function that will loop through a directory of Excel (os.walk), apply the function to each file, and save each the output from each file with an _edit.xlsx' extension ('filename*_edit**.xlsx'). I'm working with xlsxWriter.

Here's what I have to work with. This code does what I want for only one file at a time, but I would like it to apply to all files in my folder directory:

os.chdir('\\file_directory')
root ='\\file_directory'\\
allfiles = [os.path.join(root,f) for root, dirs, files in walk(root) for f in files]

#the following should be written as a function to be applied to 'allfiles' in 'root' directory
file_location ='\\file directory'
workbook = xlrd.open_workbook(file_location)
passenger = workbook.sheet_by_index(0)
hvyTruck = workbook.sheet_by_index(1)
ltTruck = workbook.sheet_by_index(2)

#create a new output workbook
output = xlsxwriter.Workbook('\\file directory\\oldWrkbkName_edit.xlsx')
out_worksheet = output.add_worksheet()    

#headers
out_worksheet.write('A1', 'StartTime')
out_worksheet.write('B1', 'N_RT')
out_worksheet.write('C1', 'N_L')
#[StartTime] 
out_worksheet.write('A2', '6:00AM')
out_worksheet.write('A3', '6:15AM')
out_worksheet.write('A4', '6:30AM')
out_worksheet.write('A5', '6:45AM')
out_worksheet.write('A6', '7:00AM')
out_worksheet.write('A7', '7:15AM')
out_worksheet.write('A8', '7:30AM')
out_worksheet.write('A9', '7:45AM')
out_worksheet.write('A10', '8:00AM')
#[N_RT]: 
out_worksheet.write('B2', (passenger.cell_value(rowx=6, colx=1) +   hvyTruck.cell_value(rowx=6, colx=1) + ltTruck.cell_value(rowx=6, colx=1)
                 + passenger.cell_value(rowx=6, colx=2) + hvyTruck.cell_value(rowx=6, colx=2) + ltTruck.cell_value(rowx=6, colx=2)))
out_worksheet.write('B3', (passenger.cell_value(rowx=7, colx=1) + hvyTruck.cell_value(rowx=7, colx=1) + ltTruck.cell_value(rowx=7, colx=1)
                 + passenger.cell_value(rowx=7, colx=2) + hvyTruck.cell_value(rowx=7, colx=2) + ltTruck.cell_value(rowx=7, colx=2)))
out_worksheet.write('B4', (passenger.cell_value(rowx=8, colx=1) + hvyTruck.cell_value(rowx=8, colx=1) + ltTruck.cell_value(rowx=8, colx=1)
                 + passenger.cell_value(rowx=8, colx=2) + hvyTruck.cell_value(rowx=8, colx=2) + ltTruck.cell_value(rowx=8, colx=2)))
out_worksheet.write('B5', (passenger.cell_value(rowx=9, colx=1) + hvyTruck.cell_value(rowx=9, colx=1) + ltTruck.cell_value(rowx=9, colx=1)
                 + passenger.cell_value(rowx=9, colx=2) + hvyTruck.cell_value(rowx=9, colx=2) + ltTruck.cell_value(rowx=9, colx=2)))
out_worksheet.write('B6', (passenger.cell_value(rowx=10, colx=1) + hvyTruck.cell_value(rowx=10, colx=1) + ltTruck.cell_value(rowx=10, colx=1)
                 + passenger.cell_value(rowx=10, colx=2) + hvyTruck.cell_value(rowx=10, colx=2) + ltTruck.cell_value(rowx=10, colx=2)))
out_worksheet.write('B7', (passenger.cell_value(rowx=11, colx=1) + hvyTruck.cell_value(rowx=11, colx=1) + ltTruck.cell_value(rowx=11, colx=1)
                 + passenger.cell_value(rowx=11, colx=2) + hvyTruck.cell_value(rowx=11, colx=2) + ltTruck.cell_value(rowx=11, colx=2)))
out_worksheet.write('B8', (passenger.cell_value(rowx=12, colx=1) + hvyTruck.cell_value(rowx=12, colx=1) + ltTruck.cell_value(rowx=12, colx=1)
                 + passenger.cell_value(rowx=12, colx=2) + hvyTruck.cell_value(rowx=12, colx=2) + ltTruck.cell_value(rowx=12, colx=2)))
out_worksheet.write('B9', (passenger.cell_value(rowx=13, colx=1) + hvyTruck.cell_value(rowx=13, colx=1) + ltTruck.cell_value(rowx=13, colx=1)
                 + passenger.cell_value(rowx=13, colx=2) + hvyTruck.cell_value(rowx=13, colx=2) + ltTruck.cell_value(rowx=13, colx=2)))
out_worksheet.write('B10', (passenger.cell_value(rowx=14, colx=1) + hvyTruck.cell_value(rowx=14, colx=1) + ltTruck.cell_value(rowx=14, colx=1)
                 + passenger.cell_value(rowx=14, colx=2) + hvyTruck.cell_value(rowx=14, colx=2) + ltTruck.cell_value(rowx=14, colx=2)))

#[N_L]:
out_worksheet.write('C2', (passenger.cell_value(rowx=6, colx=3) + hvyTruck.cell_value(rowx=6, colx=3) + ltTruck.cell_value(rowx=6, colx=3)))
out_worksheet.write('C3', (passenger.cell_value(rowx=7, colx=3) + hvyTruck.cell_value(rowx=7, colx=3) + ltTruck.cell_value(rowx=7, colx=3)))
out_worksheet.write('C4', (passenger.cell_value(rowx=8, colx=3) + hvyTruck.cell_value(rowx=8, colx=3) + ltTruck.cell_value(rowx=8, colx=3)))
out_worksheet.write('C5', (passenger.cell_value(rowx=9, colx=3) + hvyTruck.cell_value(rowx=9, colx=3) + ltTruck.cell_value(rowx=9, colx=3)))
out_worksheet.write('C6', (passenger.cell_value(rowx=10, colx=3) + hvyTruck.cell_value(rowx=10, colx=3) + ltTruck.cell_value(rowx=10, colx=3)))
out_worksheet.write('C7', (passenger.cell_value(rowx=11, colx=3) + hvyTruck.cell_value(rowx=11, colx=3) + ltTruck.cell_value(rowx=11, colx=3)))
out_worksheet.write('C8', (passenger.cell_value(rowx=12, colx=3) + hvyTruck.cell_value(rowx=12, colx=3) + ltTruck.cell_value(rowx=12, colx=3)))
out_worksheet.write('C9', (passenger.cell_value(rowx=13, colx=3) + hvyTruck.cell_value(rowx=13, colx=3) + ltTruck.cell_value(rowx=13, colx=3)))
out_worksheet.write('C10', (passenger.cell_value(rowx=14, colx=3) + hvyTruck.cell_value(rowx=14, colx=3) + ltTruck.cell_value(rowx=14, colx=3)))

output.close()

Many thanks.


Solution

  • I think this is what you want:

    (...)
    allfiles = [os.path.join(root,f) for root, dirs, files in walk(root) for f in files]
    
    for afile in allfiles:
        workbook = xlrd.open_workbook(afile)
    
        (...)
    
        output = xlsxwriter.Workbook(afile[:-5] + '_edit.xlsx')
    
        (...)
    
        output.close()