我已经安装了 openpyxl 模块,现在我正在尝试将一些数据写入一个 excel 文件,以这种方式将 excel 文件填充到一张纸中:
["01/01/2016", "05:00:00", 3] 进入第 1 行,A、B、C 列
["01/02/2016", "06:00:00", 4] 进入第 2 行,A、B、C 列
["01/03/2016", "07:00:00", 5] 进入第 3 行,A、B、C 列
["01/04/2016", "08:00:00", 6] 进入第 4 行,A、B、C 列
["01/05/2016", "09:00:00", 7] 进入第 5 行,A、B、C 列
当我尝试运行代码时,我得到以下结果:
第 1 行,A 列 - 01/05/2016
第 1 行 B 列 - 09:00:00
第 1 行,第 C - 7 列
代码:
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,))
结果:
九州编程
相关分类