We do the usual work will encounter operation excel, then write one today, how excel through the python, python operation excel library of course there are many, such as pandas, xlwt / xlrd, openpyxl, etc., each library has different distinction, specific difference, we study together under Ha.
xlrd module
xlrd Excel for reading, the operation xlrd xls / xlxs format excel
installation
xlrd is python 3rd party library needs to be installed by a pip
pip install xlrd
Read excel data
1, import xlrd Mo money
2, the instantiation of the open Excel
3, by acquiring the corresponding index table (table name can be acquired by)
4, the column, row or the coordinate data table acquired
# Coding: UTF. 8- Import to xlrd # excel path excle_path R & lt = ' E: \ 123.xlsx ' # open reading excel file Data = xlrd.open_workbook (excle_path) # selected content based on the read sheet subscript sheet = data.sheet_by_index (1 ) # obtain the total number of rows in the table nrows = sheet.nrows for I in Range (nrows): Print (sheet.row_values (I))
The above is selected to be read by the index table when we know the name of the table time, that can be read by the name of the table
# Coding: UTF. 8- Import to xlrd # excel path excle_path R & lt = ' E: \ 123.xlsx ' # open reading excel file Data = xlrd.open_workbook (excle_path) # selected content based on the read sheet subscript sheet = data.sheet_by_index (1 ) # obtain all the table names sheet_names = data.sheet_names () Print ( ' all table names ' ) Print (sheet_names) # the name of the selected sheet to read the contents Sheet1 data.sheet_by_name = ( ' name ' ) # acquired table the total number of rows nrows1 = sheet.nrows Print ( ' Name of the table of contents: ' ) for J in the Range (nrows1): Print (sheet1.row_values (J))
The above are successfully read out by name and index data, we have to read the data by the coordinates
# Coding: UTF. 8- Import to xlrd # excel path excle_path R & lt = ' E: \ 123.xlsx ' # open reading excel file Data = xlrd.open_workbook (excle_path) # selected content based on the read sheet subscript sheet = data.sheet_by_index (1 ) # is read by the coordinate A1 sheet.cell_value = (1 , 0) Print (A1)
Other uses of xlrd
1, the number of rows read sheet, the number of columns
# Excel path excle_path R & lt = ' E: \ 123.xlsx ' # open reading excel file Data = xlrd.open_workbook (excle_path) # read line data sheet, acquired by the subscripts A sheet.row_values = (. 1 ) # read column data sheet, obtained by the subscript b = sheet.col_values (1)
2, get the number of rows sheet, the number of columns
# Coding: UTF. 8- Import to xlrd # excel path excle_path R & lt = ' E: \ 123.xlsx ' # open reading excel file Data = xlrd.open_workbook (excle_path) # selected content based on the read sheet subscript sheet = data.sheet_by_index (. 1 ) Print ( ' Sheet name: {} \ nsheet columns: {} \ number nsheet lines: {} ' .format (sheet.name, sheet.ncols, sheet.nrows))
xlwt module
xlwt Excel for reading, xlwt operation is xls format excel
installation
xlwt python belonging to the third party database, needs to be installed by a pip
pip install xlwt
Write Excel data
1, first, third party libraries introduced xlwt
2. Create a workbook module, create the equivalent of a xlwt file
3. Create a table by add_sheet
4, using the write function to form a write operation
5, the data imported into Excel finish
# Coding: UTF. 8- Import xlwt # Excel path excle_path R & lt = ' E: \ 1234.xls ' # Create a Workbook module Data = xlwt.Workbook (encoding = ' UTF-. 8 ' ) # Create a table, cell_overwrite_ok = True for the does not cover the table, the default is False sheet data.add_sheet = ( ' test123 ' , cell_overwrite_ok = True) # write coordinates (0, 0) content post sheet.write (0,0, ' post ' ) # write coordinate (1,0) content software test engineer sheet.write (1,0, ' software test engineer ' ) # save to excel in data.save(excle_path)
Path to find excel in the open look and found success has been written
Other methods of Xlwt
These operations which xlwt Excel just the default method, xlwt can also change the content of the written text size, color and other operations
= xlwt.XFStyle style () # initialize style font = xlwt.Font () # create the font font.name = U ' Microsoft elegant black ' # font type font.colour_index = 6 # font color font.underline = True # underlined font. True = Italic # italics font.height = 400 # font size font size 200 is equal to excel in 10 style.font font = # set the styling
If you need to change the format of the content of what is written above, the method can be added later in writing
Let me give you a little chestnuts
# Coding: UTF. 8- Import xlwt # Excel path excle_path R & lt = ' E: \ 12314.xls ' # Create a Workbook module Data = xlwt.Workbook (encoding = ' UTF-. 8 ' ) style = xlwt.XFStyle () # initialize style font = xlwt.Font () # create the font font.name = U ' Microsoft elegant black ' # font type font.colour_index = 6 # font color font.underline = True # underlined font. = True Italic # italics font.height = 400 # font size font size 200 is equal to excel in the 10 style.font font = # to set the style # create a table, cell_overwrite_ok = True is not covered by the table, the default is False sheet = the Data. add_sheet ( ' test123 ' , cell_overwrite_ok = True) # written to coordinates (0,0) positions content sheet.write (0, 0, ' office ' , style) # write coordinates (1,0) content for the software test engineer sheet.write (1,0, ' software test engineer ' , style) # save to excel in data.save (excle_path)
Of course, Excel python operation method just so many, many more Sao operation, until the time we used to study together Kazakhstan
The feeling of quiet to write to you to help, you can point a concern for updating ~~