-1

I wrote a python code to get a string output as following code segment.

a = "First "
b = "Second"
c = a+b

I need to save value of c in a excel file every time I run that program.How can I do that?

N00b
  • 148
  • 11
Chandi jbw
  • 13
  • 3
  • 2
    Possible duplicate of [Writing to an Excel spreadsheet](https://stackoverflow.com/questions/13437727/writing-to-an-excel-spreadsheet) – Alderven Feb 19 '19 at 10:01
  • 1
    can you show what you have tried – Jeril Feb 19 '19 at 10:02
  • I tried with Pandas. But as I learned it is used for taking data from excel, CSV and some other format and save them in excel file.I just want to save string output of python program to save in excel file. – Chandi jbw Feb 19 '19 at 11:20
  • Check my new answer. – N00b Feb 19 '19 at 14:12

2 Answers2

0
from openpyxl import Workbook
a = "First "
b = "Second"
c = a+b
file_path= "temp.xlsx"

wb = Workbook()
ws = wb.active
ws.append(c)
wb.save(file_path)
PrasPJ
  • 66
  • 3
  • Thanks.This is working only if c is a list.I have another problem with this ,when I run this program again and again only current time data will available in excel file. I need to keep previous data and add new data to it when run this program. Can I have any solution for that? – Chandi jbw Feb 19 '19 at 11:12
0

Make first your excel file named test.xlsx Then use this code to load your file and write to it:

from openpyxl import load_workbook

filename= 'test.xlsx'
wb = load_workbook(filename)
file = wb.active

file.append(["data"])

wb.save(filename=filename)
N00b
  • 148
  • 11