||
# ! /usr/bin/env python3
# -*- coding: utf-8 -*-
'''
python3 find_common_line_in_two_excels.py file1.xls file2.xls output.xls
'''
from xlrd import *
import xlwt as ExcelWrite
import os
import sys
input1 = os.path.abspath(sys.argv[1])
input2 = os.path.abspath(sys.argv[2])
output = os.path.abspath(sys.argv[3])
def open_excel(excel):
try:
data = open_workbook(excel)
return data
except Exception as e:
print (str(e))
def excel_table_byindex(excel,colnameindex=0,by_index=0): #将文件按行放入列表 [[行],[行]]
data = open_excel(excel)
table = data.sheets()[by_index]
nrows = table.nrows
ncols = table.ncols
colnames = table.row_values(colnameindex)
row_list = []
for rownum in range(nrows):
row = table.row_values(rownum)
row_list.append(row)
return row_list
def main():
tables1 = excel_table_byindex(input1)
tables2 = excel_table_byindex(input2)
for i in range(len(tables1)):
if tables1[i] in tables2: #判断 文件一中的某一行是否在文件2中,是的话打印,否则打印 没有共有的行
return tables1[i]
def writeXLS(file_name):
if main():
value = [main()]
#print (value)
xls = ExcelWrite.Workbook()
sheet = xls.add_sheet("Sheet1")
for i in range(len(value)):
for j in range(0,len(value[0])):
sheet.write(i, j, value[i][j])
xls.save(file_name)
else:
print ("no common")
if __name__=="__main__":
writeXLS(output);
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-23 11:10
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社