Python Openpyxl 操作Excel

  • 新建一个sheet, 并用 append() 方法填充数据
from openpyxl import Workbook

book = Workbook()
sheet = book.active
sheet.title = 'student_score'

rows = [('chinese', 'sport', 'Math')
    (88, 46, 57),
    (89, 38, 12),
    (23, 59, 78),
    (56, 21, 98),
    (24, 18, 43),
    (34, 15, 67)
]

for row in rows:
    sheet.append(row)

book.save('numbers.xlsx')
  • 读取excel表
import openpyxl
import statistics as stats

book = openpyxl.load_workbook('numbers.xlsx', data_only=True)
sheet = book['student_score']
values = []
for row in sheet.iter_rows(values_only=True):
    values.append(row)
values = values[1:]   # skip the title
posted @   opencoder  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示