Export a Python List to Excel

Viewed 94376

I am trying to export a list to excel via the Win32COM client whihc i have imported at the header. The object i created is coded as below, but I cant seem to get it to export each value to its own row in the spreadsheet. If I can get a good pointer (other than give up python!! :D), I would appreciate it.

class XcelExport():
    def excel(self):
        app = 'Excel'
        xl = win32.gencache.EnsureDispatch('%s.Application' % app)
        ss = xl.Workbooks.Open(r'C:\MyFile.xls')
        sh = ss.ActiveSheet
        xl.Visible = True
        sleep(.1)  
        sh.Cells(1,1).Value = 'Export-to-%s : Items' % app
        sleep(.1)
        for i in EventTableRFT:
            sh.Range("A").Value = i 
        sh.Cells(i+2,1).Value = "End of the List!"

xprt = XcelExport()
xprt.excel()
3 Answers
class Signal(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def to_dict(self):
        return {
            'x': self.x,
            'y': self.y,
        }

def file_save_excel(dataset, filename, sheet_name, new_sheet):
    dataset = dataset.applymap(lambda x: x.encode('unicode_escape').
                               decode('utf-8') if isinstance(x, str) else x)

    print('Writing the processed dataset to excel file...')
    writer = pd.ExcelWriter(filename, engine='openpyxl')

    if os.path.exists(filename) and new_sheet:
        book = openpyxl.load_workbook(filename)
        writer.book = book

    dataset.to_excel(writer, sheet_name=sheet_name, index=False)
    writer.save()
    writer.close()
    return True

dataframe = pandas.DataFrame.from_records([s.to_dict() for s in signals])
file_save_excel(dataframe, 'FileName.xlsx', 'SheetName', True)
Related