0

I'm getting daily logs in a csv format and currently opening them in excel and copy/pasting into my master excel file. I'd like to make a Python script that can read a new csv file and write to the master excel file on a specific sheet and starting from a specific cell (one column over from where the last day's data was written). Alternatively, if it's easier to first save the csv file as an excel file and then write that to the master excel file, that would work too.

Edit - I ended up doing this (it's probably pretty ugly but it works for now):

from xlrd import open_workbook
from xlutils.copy import copy

def add_data(master_file, csv_file):    

    rb = open_workbook((master_file), formatting_info=True, on_demand=True)
    sheet_name = csv_file[:-4]
    total_sheets = len(rb.sheet_names())
    wb = copy(rb)
    wb.add_sheet(sheet_name)

    row = 0
    with open(csv_file, 'rb') as csv:
        reader = csv.readlines()
        for line in range(len(reader)):
            new = reader[line].split(',')
            for i in range(len(new)):
                if new[i] == '\n':
                    row += 1
                elif new[i][-1] == '\n':
                    new[i] = new[i][:-1]
                    wb.get_sheet(total_sheets).write(row,i,new[i])
                    row += 1
                else:
                    wb.get_sheet(total_sheets).write(row,i,new[i])

    wb.save('new_'+master_file)
    print sheet_name + " added successfully..."
dsapoz
  • 31
  • 3

0 Answers0