I have installed openpyxl module and now I am trying to write some data into an excel file, to fill up the excel file in one sheet in this way :
["01/01/2016", "05:00:00", 3]
into row 1, colums A, B, C
["01/02/2016", "06:00:00", 4]
into row 2, colums A, B, C
["01/03/2016", "07:00:00", 5]
into row 3, colums A, B, C
["01/04/2016", "08:00:00", 6]
into row 4, colums A, B, C
["01/05/2016", "09:00:00", 7]
into row 5, colums A, B, C
When I try running the code, I obtain the following results :
Code:
import os, sys
from openpyxl import Workbook
from datetime import datetime
dt = datetime.now()
list_values = [["01/01/2016", "05:00:00", 3],
["01/02/2016", "06:00:00", 4],
["01/03/2016", "07:00:00", 5],
["01/04/2016", "08:00:00", 6],
["01/05/2016", "09:00:00", 7]]
wb = Workbook()
sheet = wb.active
sheet.title = "Data"
row = 1
sheet['A' + str(row)] = "Date"
sheet['B' + str(row)] = "Hour"
sheet['C' + str(row)] = "Value"
for item in list_values:
sheet['A' + str(row)] = item[0]
sheet['B' + str(row)] = item[1]
sheet['C' + str(row)] = item[2]
row =+ 1
filename = 'Book_3.xlsx'
os.chdir(sys.path[0])
os.system('start excel.exe "%s\\%s"' %(sys.path[0], filename,))
You were using =+ instead of +=. Below code should work. I also change sys.path[0] to os.getcwd() which writes to the current working directory. Also added wb.save, as else code didn't produce an output.
import os, sys
from openpyxl import Workbook
from datetime import datetime
dt = datetime.now()
list_values = [["01/01/2016", "05:00:00", 3],
["01/02/2016", "06:00:00", 4],
["01/03/2016", "07:00:00", 5],
["01/04/2016", "08:00:00", 6],
["01/05/2016", "09:00:00", 7]]
wb = Workbook()
sheet = wb.active
sheet.title = "Data"
row = 1
sheet['A' + str(row)] = "Date"
sheet['B' + str(row)] = "Hour"
sheet['C' + str(row)] = "Value"
for item in list_values:
sheet['A' + str(row)] = item[0]
sheet['B' + str(row)] = item[1]
sheet['C' + str(row)] = item[2]
row += 1
dest_filename = 'Book_3.xlsx'
wb.save(filename = dest_filename)
os.chdir(os.getcwd())
os.system('start excel.exe "%s\\%s"' %(os.getcwd(), 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.