Python merges multiple Excel (based on Python 3.X)

Source: Internet
Author: User

# The following variables require you to choose according to your specific situation
title = [Id,"Project Leader",Project,"Claim for reimbursement","Content of reimbursement items *","Total Cost","Scheduled reimbursement Date","Reimbursement Person","Billing Information"]
# Where to search for multiple tables
# filelocation = "E:\\totally\\financepdf"
Filelocation =E\\Totally\\Financepdf "
# The filename suffix of the search under the current folder
Fileform =". Pdf.xlsx"
# The location where the merged table will be stored
Filedestination =E\\Totally\\"
# The merged table is named file
File ="Finance_all"

# First find out how many documents in the default folder need to be consolidated
Import Glob
Import OS
From NumPyImport *

Filearray = []
Address_excel=E\\Totally\\Financepdf "
F_list = Os.listdir (address_excel)
For FileNAMEIn F_list:
# Os.path.splitext (): Detach file name and extension
If Os.path.splitext (FileNAME) [1] = ='. xlsx ':
Filearray.append (FileNAME)
# above is to read all the Excel tables from the Pythonscripts folder and store all the names in the list Filearray
Print"%d documents in default folder"% Len (Filearray))
GE = len (filearray)
Matrix = [None] * GE
# implement read and write data

# The following is the reading of all the files into the three-dimensional list cell[][][] (not including the table header)
Import xlrd

For IInchRange (GE):
fname = Filearray[i]
BK = Xlrd.open_workbook (fname)
Try
SH = bk.sheet_by_name ("Sheet")
Except
Print"Sheet not found in file%s, read file data failed"% fname)
nrows = Sh.nrows
Matrix[i] = [0] * (nrows-1)

Ncols = Sh.ncols
For MInchRange (Nrows-1):
MATRIX[I][M] = ["0"] * ncols

For JInchRange1, nrows):
For KInchRange0, Ncols):
Matrix[i][j-1][k] = Sh.cell (j, k). Value
# below is the write data to the new form Test.xls
Import XLWT
# Here's a list of the headers
filename = xlwt. Workbook ()
Sheet = Filename.add_sheet ("Hel")

For IInchRange0, Len (title)):
If title[i][-1] = ="*":
CRS =1
Sheet. Write_merge (0,0, I, Crs+i, Title[i])
# sheet.write (0, I, title[i][-2])
Elif i>=4:
merge_leng=i+1
Sheet.write (0, Merge_leng, Title[i])
Else
Sheet.write (0+ N
# sum the previous file how many lines were written
zh = 1
for i in range (GE):
for J in range (len (matrix[i)):
for k in range (len (matrix[i][j)):
Sheet.write (Zh zh = zh + 1
print ( "I've merged%d files into 1 files, and named%s.xls. "% (Ge Filename.save (filedestination + file + ". xls ")

Python merges multiple Excel (based on Python 3.X)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.