import xlsxwriter
# 创建一个工作簿并添加一个工作表
workbook = xlsxwriter.Workbook("c.xlsx")
worksheet = workbook.add_worksheet()
# 设置列宽
worksheet.set_column("A:A", 20)
# 设置格式
bold = workbook.add_format({"bold": True})
# 设置单元格的值
worksheet.write("A1", "Hello")
# 带格式的单元格
worksheet.write("A2", "World")
# 写一些数字,用行列标识
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456, bold)
# 插入一张图片
worksheet.insert_image("B5", "s.png")
# 关闭文件流
workbook.close()
import xlrd
import xlsxwriter
source_xls = ["a.xlsx", "b.xlsx"]
target_xls = "3.xlsx"
# 读取数据
data = []
for i in c:
wb = xlrd.open_workbook(i)
for sheet in wb.sheets():
for rownum in range(sheet.nrows):
data.append(sheet.row_values(rownum))
print(data)
# 写入数据
workbook = xlsxwriter.Workbook(target_xls)
worksheet = workbook.add_worksheet()
font = workbook.add_format({"font_size": 14})
for i in range(len(data)):
for j in range(len(data[i])):
worksheet.write(i, j, data[i][j], font)
# 关闭文件流
workbook.close()
import xlrd
import xlsxwriter
source_xls = ["a.xlsx", "b.xlsx"]
target_xls = "合并.xlsx"
# 读取数据
data = []
# 重复数据
dupdata = []
# 姓名列表,按照姓名去重
name = []
# 获取excel的个数
sheetcount = len(source_xls)
i = 0
while i len(source_xls):
wb = xlrd.open_workbook(source_xls[i])
# 存储不同excel 的数据
data.append([])
# 一个excel 可能存在多张表格
for sheet in wb.sheets():
if i == 0:
# 先把表头添加进去
dupdata.append(sheet.row_values(0))
for rownum in range(sheet.nrows):
# 判断名字是否重复,表头都是姓名开头的,所以把姓名除去
if (sheet.row_values(rownum)[0] in name):
dupdata.append(sheet.row_values(rownum))
# 给数据添加另一个表的表头
if (sheet.row_values(rownum)[0] == '姓名'):
data[i].append(sheet.row_values(rownum))
else:
name.append(sheet.row_values(rownum)[0])
data[i].append(sheet.row_values(rownum))
i+=1
# 合并数据
workbook = xlsxwriter.Workbook(target_xls)
worksheet = workbook.add_worksheet()
font = workbook.add_format({"font_size": 14})
lineNum = 0
for num in range(len(data)):
# 区分来自不同excel 的数据
if num== 0 :
for i in range(len(data[num])):
# 姓名
worksheet.write(lineNum, 0, data[num][i][0], font)
# 检查编号
worksheet.write(lineNum, 1, data[num][i][1], font)
# 年龄
worksheet.write(lineNum, 2, data[num][i][23], font)
# 蓝标
worksheet.write(lineNum, 3, data[num][i][14], font)
# 黄标
worksheet.write(lineNum, 4, data[num][i][19], font)
worksheet.write(lineNum, 5, data[num][i][20], font)
worksheet.write(lineNum, 6, data[num][i][21], font)
# 大小
worksheet.write(lineNum, 7, data[num][i][24], font)
worksheet.write(lineNum, 8, data[num][i][25], font)
worksheet.write(lineNum, 9, data[num][i][26], font)
lineNum += 1
# 只有两个excel ,所以直接用了 else
else:
for i in range(len(data[num])):
lineNum += 1
# 姓名
worksheet.write(lineNum, 0, data[num][i][0], font)
# 检查编号
worksheet.write(lineNum, 1, data[num][i][1], font)
# 年龄
worksheet.write(lineNum, 2, data[num][i][2], font)
# 蓝标
worksheet.write(lineNum, 3, data[num][i][30], font)
# 关闭文件流
workbook.close()
# 针对重复数据,连表头一起输出,方便后续回顾查看
workbook = xlsxwriter.Workbook("重复.xlsx")
worksheet = workbook.add_worksheet()
font = workbook.add_format({"font_size": 14})
for i in range(len(dupdata)):
for j in range(len(dupdata[i])):
worksheet.write(i, j, dupdata[i][j], font)
# 关闭文件流
workbook.close()
到此这篇关于教你用Python代码实现合并excel文件的文章就介绍到这了,更多相关Python合并excel文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!