Mounting module
1, find the corresponding module http://www.python-excel.org/
2. Installing with PIP Install
Pip Install Xlrd
Pip Install Xlsxwriter
Pip List View
Xlsxwriter Example
1 ImportXlsxwriter2 3 #Create a workbook and add a worksheet4Workbook = Xlsxwriter. Workbook ("demo.xlsx")5Worksheet =Workbook.add_worksheet ()6 7 #Set column widths8Worksheet.set_column ("a:a", 20)9 Ten #Set Format OneBold = Workbook.add_format ({"Bold": True}) A - #set the value of a cell -Worksheet.write ("A1","Hello") the - #formatted Cells -Worksheet.write ("A2"," World") - + #write some numbers, identify them with rows and columns -Worksheet.write (2, 0, 123) +Worksheet.write (3, 0, 123.456, Bold) A at #Insert a picture -Worksheet.insert_image ("B5","C:/users/cheng/desktop/1.png") - - #Close File Stream -Workbook.close ()
The results of the operation are as follows:
Thinking of merging Excel data
Excel is made up of rows and columns, so here all the data in all the sheet in all the files is read out to form a two-dimensional array and then write to the new Excel
Code
1 Importxlrd2 ImportXlsxwriter3 4Source_xls = ["d:/python/1.xlsx","d:/python/2.xlsx"]5Target_xls ="d:/python/3.xlsx"6 7 #reading Data8data = []9 forIinchSource_xls:TenWB =Xlrd.open_workbook (i) One forSheetinchwb.sheets (): A forRowNuminchRange (sheet.nrows): - data.append (Sheet.row_values (rownum)) - Print(data) the #Write Data -Workbook =Xlsxwriter. Workbook (Target_xls) -Worksheet =Workbook.add_worksheet () -Font = Workbook.add_format ({"font_size": 14}) + forIinchRange (len (data)): - forJinchRange (len (data[i)): + Worksheet.write (i, J, Data[i][j], font) A #Close File Stream atWorkbook.close ()
Run results
Experience
Just started to learn python, probably because of the Java code to see the reason, beginners feel grammar strange, and Java some similar, and feel and javascript,perl this kind of like ...
It's an object-oriented, interpretive, high-level programming language!!!
Python merges multiple Excel data