https://blog.csdn.net/weixin_43094965/article/details/82226263
import datetime from random import choice from time import time from openpyxl import load_workbook from openpyxl.utils import get_column_letter # 设置文件 mingc addr = r"d:openpyxl.xlsx" # 打开文件 wb = load_workbook(addr) # 创建一张新表 ws = wb.create_sheet() # 第一行输入 ws.append(['TIME', 'TITLE', 'A-Z']) # 输入内容(500行数据) for i in range(500): TIME = datetime.datetime.now().strftime("%H:%M:%S") TITLE = str(time()) A_Z = get_column_letter(choice(range(1, 50))) ws.append([TIME, TITLE, A_Z]) # 获取最大行 row_max = ws.max_row # 获取最大列 con_max = ws.max_column # 把上面写入内容打印在控制台 for j in ws.rows: # we.rows 获取每一行数据 for n in j: print(n.value, end=" ") # n.value 获取单元格的值 print() # 保存,save(必须要写文件名(绝对地址)默认 py 同级目录下,只支持 xlsx 格式) wb.save(addr) wb.close()