1 回答
TA贡献1785条经验 获得超4个赞
您使用的是 =+ 而不是 +=。下面的代码应该可以工作。我还将 sys.path[0] 更改为 os.getcwd() 写入当前工作目录。还添加了 wb.save,因为其他代码没有产生输出。
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,))
添加回答
举报