Home » excel » Write data(list) into one excel file as row wise using python

Write data(list) into one excel file as row wise using python

Posted by: admin April 23, 2020 Leave a comment

Questions:

As i’m trying to append my list values into one excel file as row wise i tried but it appending as column wise.

my INPUT is:

[[1 1 1 'Chair' 1 50 'hosur' Timestamp('2017-01-01 00:00:00')]
 [2 1 1 'Table' 1 50 'hosur' Timestamp('2017-01-02 00:00:00')]]

i want to append it into one excel file as ROW wise not COLUMN wise
my tried code is

    import pandas as pd
    df = pd.DataFrame.from_dict({'row1':[[1 1 1 'Chair' 1 50 'hosur' Timestamp('2017-01-01 00:00:00')]
 ,'row2':[2 1 1 'Table' 1 50 'hosur' Timestamp('2017-01-02 00:00:00')]]})
    df.to_excel('test.xlsx', header=True, index=False)
How to&Answers:

Hope this helps You.. Try like this. Here I iterated the list within a list and appended the value.

expenses =     [[1, 1, 1, 'Chair', 1, 50, 'hosur', 'Timestamp(2017-01-01 00:00:00)'],
     [2,1, 1, 'Table', 1, 50, 'hosur','Timestamp(2017-01-02 00:00:00)']]

import openpyxl

wb = openpyxl.Workbook()

sheet = wb.active

le_ = len(expenses)
p = 0

for i in expenses:
     k = 0
     for j in i:
          c1 = sheet.cell(row=p+1,column=k+1)
          c1.value = str(j)
          k+=1
     p+=1

wb.save("demo1.xlsx")