自用代码
import openpyxl
import os
# 第一步:打开工作簿
basedir = os.path.dirname(__file__)
upload_path = os.path.join(basedir, 'data.xlsx')
upload_path2 = os.path.join(basedir, 'product.xlsx')
wb = openpyxl.load_workbook(upload_path, data_only=True)
wb2 = openpyxl.load_workbook(upload_path2, data_only=True)
# 第二步:选取表单
sh = wb['出']
sh2 = wb2['1']
print(sh.cell(1, 1).value)
print(sh2.cell(1, 1).value)
# 第三步:读取数据
# 参数 row:行 column:列
for i in range(1702):
code1 = sh.cell(i + 2, 2).value
# print(code1)
for j in range(334):
zx = sh2.cell(j + 2, 1).value
# print(zx)
try:
code2 = int(zx)
code1 = int(code1)
if code1 == code2:
print(code1)
print(i + 2)
# sh2.cell(j + 2, 8).value = sh.cell(i + 2, 3).value
# sh2.cell(j + 2, 9).value = sh.cell(i + 2, 5).value
sh.cell(i + 2, 3).value = sh2.cell(j + 2, 5).value
sh2.cell(i + 2, 7).value = "写入成功"
# sh.cell(i + 2, 9).value = sh2.cell(j + 2, 5).value
except:
pass
wb.save('a1.xlsx')
wb2.save('a2.xlsx')
# print(sh['B3'] .value)
# # 按行读取数据 list(sh.rows)
# print(list(sh.rows)[1:]) # 按行读取数据,去掉第一行的表头信息数据
# for cases in list(sh.rows)[1:]:
# case_id = cases[0].value
# case_excepted = cases[1].value
# case_data = cases[2].value
# print(case_excepted,case_data)
# # 关闭工作薄
# wb.close()
python操作excel数据对比
最新推荐文章于 2024-09-02 23:14:47 发布