Python handles operation code to execl

1. Read execl

  1. The prerequisite is to install the xlrd module. You can find the installation tutorial on the Internet, so I won’t write it here.

  2. Open form

  3. Read sheet

  4. Read data by row or column or cell

Actual operation:

import xlrd
import xlwt
from xlutils.copy import copy
def info_row_col_execl(file_name):
myworkbook = xlrd.open_workbook(file_name)
mySheetlist = myworkbook.sheets()
mysheet = mySheetlist[0]
nrows = mysheet.nrows
ncols = mysheet.ncols
return nrows,ncols
def read_execl(filename):
myworkbook = xlrd.open_workbook(filename)
mySheetlist = myworkbook.sheets()
mysheet = mySheetlist[0]return mysheet
def com_exel(file_name_dai,file_name_all):
dai_rows, dai_cols =info_row_col_execl(file_name_dai)
all_rows,all_cols =info_row_col_execl(file_name_all)print(dai_rows, dai_cols)print(all_rows,all_cols)
def read_execl_values(file_name):
list_execl_values =[]
mysheet =read_execl(file_name)
execl_rows, execl_cols =info_row_col_execl(file_name)
myRowValues_name= mysheet.row_values(0)for nrow inrange(1,execl_rows):
myRowValues= mysheet.row_values(nrow)
# print(myRowValues)
myRow_name_value=dict(zip(myRowValues_name,myRowValues))
list_execl_values.append(myRow_name_value)return list_execl_values
def com_execl_values(file_name_dai,file_name_all,key_list):
dai_list_execl_values =read_execl_values(file_name_dai)
all_list_execl_values =read_execl_values(file_name_all)
row_num =1for i in dai_list_execl_values:
dai_list_execl_value = i
dai_len_row =len(dai_list_execl_value)for k in all_list_execl_values:
all_list_execl_value = k
result =compare_two_dict(dai_list_execl_value,all_list_execl_value,key_list)if result =="Pass":
# print(dai_list_execl_value,all_list_execl_value,result)print(dai_list_execl_value['ID'],dai_list_execl_value['first name'],)print(all_list_execl_value['class'],all_list_execl_value['teacher'])write_execl(file_name_dai,all_list_execl_value['class'],all_list_execl_value['teacher'],row_num,dai_len_row)break;
row_num = row_num +1   
def write_execl(file_name_dai,all_list_execl_value_1,all_list_execl_value_2,row_num,dai_len_row):
open_execl = xlrd.open_workbook(file_name_dai)
open_execl_copy =copy(open_execl)
open_execl_copy_sheet = open_execl_copy.get_sheet(0)
open_execl_copy_sheet.write(row_num,dai_len_row-3,all_list_execl_value_1)
open_execl_copy_sheet.write(row_num,dai_len_row-2,all_list_execl_value_2)
open_execl_copy.save(file_name_dai)
def compare_two_dict(dai_dict,all_dict,key_list):
flag = True
dai_dict_key = dai_dict.keys()
all_dict_key = all_dict.keys()iflen(key_list)!=0:for key in key_list:if key in dai_dict_key and key in all_dict_key:if dai_dict[key]== all_dict[key]:
# print(dai_dict[key],all_dict[key])
flag = flag & True
else:
flag = flag & False
else:
raise Exception('key_list contains error key')else:
raise Exception('key_list is null')if flag:
result ="Pass"else:
result ="Failed"
# print(result)return result
if __name__ =='__main__':
file_name_dai ='name_1.xlsx'
file_name_all ='name.xlsx'
key_list =['ID','first name']com_execl_values(file_name_dai,file_name_all,key_list)

to sum up

So far, this article about python's processing operation code for execl is introduced. For more related python's processing of execl, please search for ZaLou.Cn's previous articles or continue to browse the related articles below. Hope you will support ZaLou.Cn more in the future. !

Recommended Posts

Python handles operation code to execl
Python code to find bugs (2)
Python code to find bugs(7)
How to comment python code
Python code to find bugs(4)
Python code to find bugs (3)
Python code to find bugs(9)
Python code to find bugs(6)
Python code to find bugs (1)
Python code to find bugs(8)
python how to view webpage code
How to wrap in python code
200 lines of Python code to achieve snake
Does Python code need to be indented
Python file and directory operation code summary
Python file operation
01. Introduction to Python
python operation kafka
Introduction to Python
Actually very simple | Python code to find bugs (12)
How to use code running assistant in python
500 lines of python code to achieve aircraft war
How to set code auto prompt in python
Python how to move the code collectively right
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
Python operation SQLite database
Python SMS bombing code
Python operation yaml instructions
Minimalism is the soul of Python | Python code to find bugs (10)
Python automated operation and maintenance 2
Solution to python alignment error
Python operation Excel merge cells
CentOS upgrade python2 to pyth
How to write Pythonic code
Python drawing rose implementation code
Python negative modulus operation example
How to learn python quickly
How to uninstall python plugin
Python regular expression example code
Python implements code block folding
Python automated operation and maintenance 1
Python install OpenCV sample code
3 ways to encrypt Python files
Python implements verification code recognition
How to understand python objects
How to use python tuples
Is python code case sensitive
Quick start Python file operation