Excel processing test data (base)

Operation Excel, use third-party libraries, others use the code written,

This is done using openpyxl operation after version 2010 XLSX format.

Some 2003 classic version is not available, only use other libraries such as: xlrd, xlwt.

Tablib which we can support, but its relatively large dependence, use copy, interested partners can own Baidu small 

First, install openpyxl

pip install openpyxl

Second, import openpyxl

import openpyxl 

1, open the excel file gets workbook (file contents, see the bottom screenshot)

# There is a mandatory, path or file object can fill in documents 
wb = openpyxl.load_workbook ( ' D: \ datas.xlsx ' )
 Print (wb)

2, to obtain the form (a excel file may have been a form)

# Get all forms 
Sheet = wb.worksheets
 Print (Sheet)
# Acquired by the index sheet, the index starts from 0 
Sheet = wb.worksheets [0] 
Print (Sheet)
# Get through the form name form. There is a problem, then use the sheet can not point out the tips 
sheet = wb.get_sheet_by_name ( ' Sheet1 ' )
 Print (sheet)
# Simple version of the dictionary in the form. The above problems and as 
Sheet WB = [ ' Sheet1 ' ]
 Print (Sheet)

3, obtaining cell data

  1, obtaining a cell.

        Acquired data is a common character string or a number.

# Get the value of a certain cell. () Number of the first transmission line row cell, the number of columns of the second column pass, 
# cell to start at 1, the index and distinguish python 
Cell sheet.cell = (1, 1 ). value
 Print (the Cell)   # url

 

  2, obtains a line / one cell.

        Acquiring a plurality of data, usually with multiple rows of data storage, lists, dictionaries, tuples.

# Get the data of one line 
Cell Sheet = [. 1 ]
 Print (Cell)    # objects pointed acquired a tuple, which is a cell containing a target parentheses 
#   output data (<Cell 'Sheet1'.A1>, < Cell' Sheet1 '.B1>, <the Cell' Sheet1'.C1>) 

# acquiring data of a 
Cell Sheet = [ ' a ' ]
 Print (Cell)

 

  3, obtaining a plurality of rows / columns of cells.

        The resulting data is generally nested data, such as nested lists

 

# Get all the rows, the easiest way 
Data = sheet.rows 
DATA_LIST = List (Data) [. 1:]   # excluding the first data, the first title

# Get all data 
DATA_LIST = List (sheet.rows) [. 1:]   # excluding the first data, the first title 
new_data = []   # new list storing data # 
for Row in DATA_LIST:
     # each row of data stored emptied once per cycle 
    ROW_DATA = []
     # the sequentially extracted data row inside out 
    for Cell in row:
         # add value to ROW_DATA list 
        row_data.append (Cell.Value)
     # no cycle every time data is added to new_data 
    new_data. the append (ROW_DATA)
 Print (new_data)
 

 

4, write, be sure to save the finish after using the file you want to turn off ( to use before writing the code must be run to close the file, otherwise it will error )

# Write 
sheet.cell (2, 1) .Value = ' 12306 ' 

# save save to provide a path, a path that is not before the Save As wb.save (r ' D: \ datas.xlsx ' )
# close wb.close ()

 

Guess you like

Origin www.cnblogs.com/yongzhuang/p/12177449.html