方法有很多,pandas可以读取Excel数据,创建Excel表格,写入数据,但是好像不能实现追加,后写入的数据会覆盖到以前数据;xlrd、xlwt、xlutils可是实现对xls格式的表格数据的读取、写入以及修改(如:追加);openpyxl可以实现对xlsx格式的表格数据进行读、写、修改等操作。本例仅以表格的写入与追加为例,简单说明一下Python对Excel的操作,详细可参考上面这些工具包的说明文档。
import pandas as pd
import numpy as np
df = pd.DataFrame(
{
'name': ['Jack', 'kate', 'Jim', 'Steve', 'Mike', 'Lucy'],
'Gender': ['M', 'F', 'M', 'M', 'M', 'F'],
'Age': [17, 15, 22, 34, 18, 23],
'Height': [173, 168, 164, 180, 182, 190],
'Hobby': ['Reading', 'Music', 'Football', 'Reading', 'Cooking', 'Running']
}
)
df
name | Gender | Age | Height | Hobby | |
---|---|---|---|---|---|
0 | Jack | M | 17 | 173 | Reading |
1 | kate | F | 15 | 168 | Music |
2 | Jim | M | 22 | 164 | Football |
3 | Steve | M | 34 | 180 | Reading |
4 | Mike | M | 18 | 182 | Cooking |
5 | Lucy | F | 23 | 190 | Running |
df1 = df.copy()
df2 = df.copy()
df3 = df.copy()
df的追加,设置ignore_index=True可以自动递增索引号
df4 = df.append(df1, ignore_index=True)
df4
name | Gender | Age | Height | Hobby | |
---|---|---|---|---|---|
0 | Jack | M | 17 | 173 | Reading |
1 | kate | F | 15 | 168 | Music |
2 | Jim | M | 22 | 164 | Football |
3 | Steve | M | 34 | 180 | Reading |
4 | Mike | M | 18 | 182 | Cooking |
5 | Lucy | F | 23 | 190 | Running |
6 | Jack | M | 17 | 173 | Reading |
7 | kate | F | 15 | 168 | Music |
8 | Jim | M | 22 | 164 | Football |
9 | Steve | M | 34 | 180 | Reading |
10 | Mike | M | 18 | 182 | Cooking |
11 | Lucy | F | 23 | 190 | Running |
import xlrd # 读取
import xlwt # 写入
from xlutils.copy import copy # 修改(追加写入):xlutils
def write2xls(path, sheetname, value):
index = len(value) # 获取需要写入数据的行数
workbook = xlwt.Workbook() # 创建一个工作簿
sheet = workbook.add_sheet(sheetname) # 在工作簿中新建一个表格
for i in range(index):
for j in range(len(value[i])):
sheet.write(i, j, value[i][j]) # 向表格中写入数据(对应的行和列)
workbook.save(path)
print("xls格式表格写入成功!")
def append2xls(path, sheetname, value):
index = len(value) # 获取需要写入的数据的行数
workbook = xlrd.open_workbook(path) # 打开工作簿
# sheets = workbook.sheet_names() # 获取工作簿里的所有表格
worksheet = workbook.sheet_by_name(sheetname) # 获取工作簿中所有表格中的第一个表格
rows_old = worksheet.nrows # 获取表格中已存在的数据的行数
new_workbook = copy(workbook) # 将xlrd对象拷贝转化为xlwt对象
new_worksheet = new_workbook.get_sheet(0) # 获取转化后工作簿中的第一个表格
for i in range(index):
for j in range(len(value[i])):
new_worksheet.write(i+rows_old, j, value[i][j]) # 追加写入数据,注意从 i+rows_old 行开始
new_workbook.save(path)
print("xls格式表格追加写入成功!")
column_name = np.array([df4.columns])
write2xls(path='./zx_test.xls', sheetname='text', value=column_name)
append2xls(path='./zx_test.xls', sheetname='text', value=df1.to_numpy())
append2xls(path='./zx_test.xls', sheetname='text', value=df4.to_numpy())
xls格式表格写入成功!
xls格式表格追加写入成功!
xls格式表格追加写入成功!
import openpyxl
def write2xlsx(path, sheetname, value):
index = len(value)
workbook = openpyxl.Workbook() # 实例化
sheet = workbook.active # 激活worksheet
sheet.title = sheetname
for i in range(index):
for j in range(len(value[i])):
sheet.cell(row=i+1, column=j+1, value=str(value[i][j]))
workbook.save(path)
print("xlsx格式表格数据写入成功!")
def append2xlsx(path, sheetname, value):
index = len(value)
workbook = openpyxl.load_workbook(path)
sheet = workbook[sheetname]
for i in range(index):
sheet.append(list(value[i])) # append的内容必须是可迭代对象,里面的value必须是str类型
workbook.save(path)
print("xlsx格式表格数据追加成功!")
column_name = np.array([df4.columns])
write2xlsx(path='./zx_test_2.xlsx', sheetname='text', value=column_name)
data = df4.to_numpy()
append2xlsx(path='./zx_test_2.xlsx', sheetname='text', value=data)
data2 = df1.to_numpy()
append2xlsx(path='./zx_test_2.xlsx', sheetname='text', value=data2)
xlsx格式表格数据写入成功!
xlsx格式表格数据追加成功!
xlsx格式表格数据追加成功!