import xlsxwriter
# 創(chuàng)建一個工作簿并添加一個工作表
workbook = xlsxwriter.Workbook("c.xlsx")
worksheet = workbook.add_worksheet()
# 設(shè)置列寬
worksheet.set_column("A:A", 20)
# 設(shè)置格式
bold = workbook.add_format({"bold": True})
# 設(shè)置單元格的值
worksheet.write("A1", "Hello")
# 帶格式的單元格
worksheet.write("A2", "World")
# 寫一些數(shù)字,用行列標識
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456, bold)
# 插入一張圖片
worksheet.insert_image("B5", "s.png")
# 關(guān)閉文件流
workbook.close()
import xlrd
import xlsxwriter
source_xls = ["a.xlsx", "b.xlsx"]
target_xls = "3.xlsx"
# 讀取數(shù)據(jù)
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)
# 寫入數(shù)據(jù)
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)
# 關(guān)閉文件流
workbook.close()
import xlrd
import xlsxwriter
source_xls = ["a.xlsx", "b.xlsx"]
target_xls = "合并.xlsx"
# 讀取數(shù)據(jù)
data = []
# 重復(fù)數(shù)據(jù)
dupdata = []
# 姓名列表,按照姓名去重
name = []
# 獲取excel的個數(shù)
sheetcount = len(source_xls)
i = 0
while i len(source_xls):
wb = xlrd.open_workbook(source_xls[i])
# 存儲不同excel 的數(shù)據(jù)
data.append([])
# 一個excel 可能存在多張表格
for sheet in wb.sheets():
if i == 0:
# 先把表頭添加進去
dupdata.append(sheet.row_values(0))
for rownum in range(sheet.nrows):
# 判斷名字是否重復(fù),表頭都是姓名開頭的,所以把姓名除去
if (sheet.row_values(rownum)[0] in name):
dupdata.append(sheet.row_values(rownum))
# 給數(shù)據(jù)添加另一個表的表頭
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
# 合并數(shù)據(jù)
workbook = xlsxwriter.Workbook(target_xls)
worksheet = workbook.add_worksheet()
font = workbook.add_format({"font_size": 14})
lineNum = 0
for num in range(len(data)):
# 區(qū)分來自不同excel 的數(shù)據(jù)
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)
# 關(guān)閉文件流
workbook.close()
# 針對重復(fù)數(shù)據(jù),連表頭一起輸出,方便后續(xù)回顧查看
workbook = xlsxwriter.Workbook("重復(fù).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)
# 關(guān)閉文件流
workbook.close()
到此這篇關(guān)于教你用Python代碼實現(xiàn)合并excel文件的文章就介紹到這了,更多相關(guān)Python合并excel文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!