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 @ 2024-11-16 21:38  opencoder  阅读(2)  评论(0编辑  收藏  举报