简体   繁体   中英

Save data into excel file in Python

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?

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM