Python操作execl文件,根据两个文件的索引,判断当索引里的行中的信息相同时,把该文件中想要的信息,复制另一个文件对应的那行上。Python版本



#!/usr/bin/env python
# -*- coding: utf-8 -*-   #You can use the chinese language by this.
import os
import sys
import xlwt
import xlrd
import xdrlib
from xlutils.copy import copy 
from xlwt import Style          #Add the plug-in


in_put=input('Please enter input file name:')
in_put_index_col=int(input('Please enter input index col :'))
in_put_col_data=int(input('Please enter input data col :'))
in_put_row_beginrange=int(input('Please enter input row begin range :'))
in_put_row_endrange=int(input('Please enter input row end range :'))

out_put=input('Please enter output file name:')
out_put_index_col=int(input('Please enter output index col :'))
out_put_col_data=int(input('Please enter output data col :'))
out_put_row_beginrange=int(input('Please enter output row begin range :'))
out_put_row_endrange=int(input('Please enter output row end range :'))

out_data = xlrd.open_workbook(out_put,formatting_info=True) # Get the out_data object of the Execl file
get_out_index=out_data.sheet_by_index(0)           #Obtaining sheet objects by sheet index

in_data= xlrd.open_workbook(in_put,formatting_info=True)    # Get the in_data object of the Execl file
get_in_data=in_data.sheet_by_index(0)              #Obtaining sheet objects by sheet index

#book=xlwt.Workbook(encoding='utf-8',style_compression=0)
#sheet=book.add_sheet('Sheet1',cell_overwrite_ok=True)
co_file=copy(out_data)          #Copy output file format
getsheet=co_file.get_sheet(0)   #Get the sheet of the output file

for j in  range(in_put_row_beginrange,in_put_row_endrange):             #input start 8row ,end 33row
   for i in  range(out_put_row_beginrange,out_put_row_endrange):         #output start 4row ,end 102row
      in_stud_numb=get_in_data.cell_value(j,in_put_index_col)                 #0  5       input index col
      accomplishment=get_in_data.cell_value(j,in_put_col_data)                #4  9    input data  col
      out_stud_numb=get_out_index.cell_value(i,out_put_index_col)         #2      output index col
      if in_stud_numb==out_stud_numb:                             #Whether the comparison index is the same
         getsheet.write(i,out_put_col_data,accomplishment,Style.default_style)         #15 write the output data col
co_file.save(out_put)             #save the output file

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值