Using the xlrd module and the xlwt module
Solution Idea: xlwt module is non-appendable to write .xls, so with the help of for loops and lists, to write at once, so that there is no appendable and non-appendable.
And merge Excel tables, each Excel table as a line, that is, line merge, another idea, the Excel table labels as columns, can be merged columns, that is, merge the same labels in different files composed of different labels, you can first merge the same labels in different files, the same labels in different files to form a list, and then merge the front of the composition of the different labels, you can get all the Excel file content.
The source code is below:
# Import xlrd and xlwt modules The #xlrd module is the module that reads .xls Excel files, and the xlwt module is the module that writes .xls Excel files in a non-appendable manner import xlrd,xlwt # Import the path to the file to be read a=["C:/Users/Desktop/","C:/Users/Desktop/"] # Define the path to the file to be written b="C:/Users/Desktop/" # Define a function that determines if an exception occurs when opening a file. def open(fileaddress): try: # Open an Excel file through the open_workbook() method of the xlrd module, defining the variable fo as the file handle. fo=xlrd.open_workbook(fileaddress) return fo except Exception as e: print("error!"+str(e)) # Define a function to get the number of rows of a label under a certain Excel file def getrows(fo,num): table=()[num] #sheets () function for the xlrd module comes with a function to return to the Excel file in the form of a list of labels n= #nrows is a function that comes with the xlrd module, and can be called from a label to get the number of rows in that label. return n """ Define a function that can be obtained through the Excel file path and the serial number of the label Any Excel file any label content, and return in the form of a list of """ def getsheetcontents(fileaddress,num): avalue=[] fo=open(fileaddress) table=()[num] n= for i in range(0,n): """ row_values()function asxlrdFunctions that come with the module,Called through one of the tags, The row data for this tag is available,and these rows are returned as a list,tab A column is an element of a list """ rdate=table.row_values(i) (rdate) return avalue svalue=[] k=[] # Get the number of labels of all Excel files in a for loop and return them as a list. for i in a: fo=open(i) (len(())) # Sort these label counts in ascending order () # Put all the contents of these Excel files with different labels in the list svalue by a for loop for i in range(0,k[len(k)-1]): # Put the same label from different Excel files in the list bvalue via a for loop bvalue=[] for j in a: print("Reading."+str(j)+"the first"+str(i)+"A label.") (getsheetcontents(j,i)) (bvalue) Inside #svalue are the contents of all Excel files. # Through the xlwt module Workbook () to create a .xls file, define a variable fw for the file handle fw=() line=0 The #add_sheet() function is a function that comes with the xlwt module to get a customized label # Define the variable ws as a label handle ws=fw.add_sheet("sheet1") """ Iterate through the svalue list with a for loop, and write the final data from the data from a file or a row or a column of a tag into a new tag. """ for i in range(0,len(svalue)): for j in range(0,len(svalue[i])): for m in range(0,len(svalue[i][j])): for n in range(0,len(svalue[i][j][m])): (line,n,svalue[i][j][m][n]) # Get an increasing number of rows by line line+=1 print("Consolidation complete") #save () function for xlwt comes with the function, will be merged into a good Excel file saved to a certain path (b) #xlrdmodules andxlwtNone of the modulesclose()function (math.),I.e., you can use these two modules to open a file without closing it.
to this article on the Python merge Excel table (multi-sheet) implementation of the article is introduced to this, more related Python merge Excel content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!