Openpyxl Bulk Data Writing: Complete Guide

Bulk writing data can be easily accomplished using openpyxl. Here is an example code:

from openpyxl import Workbook

# 创建一个新的工作簿
wb = Workbook()

# 获取第一个工作表
ws = wb.active

# 定义要写入的数据
data = [
    ['姓名', '年龄', '性别'],
    ['张三', 20, '男'],
    ['李四', 21, '女'],
    ['王五', 22, '男'],
]

# 批量写入数据
for row in data:
    ws.append(row)

# 保存工作簿
wb.save('example.xlsx')

This code creates a new workbook and bulk writes data to the first worksheet. Data is passed through a two-dimensional list, where each sublist represents a row of data. Within a loop, each row of data is written to the worksheet using the ws.append() method. Finally, the workbook is saved to a file using the wb.save() method.

bannerAds