Access individual cells
WS = C [ ' A4 ' ] # returns the cell A4, if the cell does not exist, it is created automatically WS [ ' A4 ' ]. 4 = # is assigned cell A4. 4 D = ws.cell (Row =. 4, = 2 column, value = 10) # to B4 assigned to the cell 10
When a worksheet created in memory, does not contain any cell, only when the first visit would be created
when the function to access large quantities of cells through cell (), although these cells have not been assigned yet these cells already created in memory
for X in Range (1,101 ): for Y in Range (1,101 ): ws.cell (Row = X, Y = column) # creates 100 * 100 cells in memory
Access to multiple cells
CELL_RANGE = WS [ ' A1 ' : ' C2 ' ] # access from A1 to C2 of all cells colC = WS [ ' C ' ] # access column C all cells col_range = WS [ ' C: D ' ] # access all the cells in column C and row D row10 = WS [10] # all the cells of row 10 access row_range WS = [5:10] # access line 5 to line 10 of all of the cells # use Worksheet.iter_rows () method to the behavior unit, traversing the plurality of cells for Row in ws.iter_rows (min_row =. 1, max_col =. 3, MAX_ROW = 2 ): for cell in row: print(cell) ''' 输出: <Cell Sheet1.A1> <Cell Sheet1.B1> <Cell Sheet1.C1> <Cell Sheet1.A2> <Cell Sheet1.B2> <Cell Sheet1.C2> ''' #使用Worksheet.iter_cols()方法以列为单位,遍历多个单元格 for col in ws.iter_cols(min_row=1, max_col=3, max_row=2): for cell in col: print(cell) ''' 输出: <Cell Sheet1.A1> <Cell Sheet1.A2> <Cell Sheet1.B1> <Cell Sheet1.B2> <Cell Sheet1.C1> <Cell Sheet1.C2> '''
For performance reasons, Worksheet.iter_cols () in read-only mode is not available
I think this may be because the size of the memory is limited, due to the size of the file can not be predicted in advance, if the file has a billion lines data
is iter_cols method at the time to traverse the file as a unit, the first to traverse the first column, may traverse the ten millionth row, memory is no longer enough
The bulk access cell
ws['C9'] = 'hello world' tuple(ws.rows) #获取A1到C9的所有单元格,以行为单位 ''' 输出: ((<Cell Sheet.A1>, <Cell Sheet.B1>, <Cell Sheet.C1>), (<Cell Sheet.A2>, <Cell Sheet.B2>, <Cell Sheet.C2>), (<Cell Sheet.A3>, <Cell Sheet.B3>, <Cell Sheet.C3>), (<Cell Sheet.A4>, <Cell Sheet.B4>, <Cell Sheet.C4>), (<Cell Sheet.A5>, <Cell Sheet.B5>, <Cell Sheet.C5>), (<Cell Sheet.A6>, <Cell Sheet.B6>, <Cell Sheet.C6>), (<Cell Sheet.A7>, <Cell Sheet.B7>, <Cell Sheet.C7>), (<Cell Sheet.A8>, <Cell Sheet.B8>, <Cell Sheet.C8>), (<Cell Sheet.A9>, <Cell Sheet.B9>, <Cell Sheet.C9>)) ''' tuple(ws.columns) #获取A1到C9的所有单元格,以列为单位 ''' 输出: ((<Cell Sheet.A1>, <Cell Sheet.A2>, <Cell Sheet.A3>, <Cell Sheet.A4>, <Cell Sheet.A5>, <Cell Sheet.A6>, ... <Cell Sheet.B7>, <Cell Sheet.B8>, <Cell Sheet.B9>), (<Cell Sheet.C1>, <Cell Sheet.C2>, <Cell Sheet.C3>, <Cell Sheet.C4>, <Cell Sheet.C5>, <Cell Sheet.C6>, <Cell Sheet.C7>, <Cell Sheet.C8>, <Cell Sheet.C9>)) '''
For performance reasons, Worksheet.columns in read-only mode is unavailable
Cell value processing
value if the value of the cell to process only, can use the property Worksheet.values, which returns only the cell
# This method iterates only cell values for Row in ws.values: for value in Row: Print (value) # Worksheet.iter_rows () and Worksheet.iter_cols () method can be acquired by way of the development of the cell parameters only value for Row in ws.iter_rows (min_row =. 1, max_col =. 3, MAX_ROW = 2, values_only = True): Print (Row) '' ' output: (None, None, None) (None, None, None) ' ' '
data storage
WS = C [ ' A4 ' ] c.value = ' Hello, World ' # to the assigned cell A4
Save the file
using the save () function is the easiest and safest way
wb = Workbook() wb.save('balances.xlsx')
Note:
1) saved in this way will overwrite the original file of the same name without warning, so be careful
extension 2) file does not have to xlsx, but if not, could lead to open office
Save streaming
If you save the file into a stream, such as when using the Pyramid, Flask or Django and other applications, you can simply provide a NamedTemporaryFile () function
from tempfile Import NamedTemporaryFile from openpyxl Import Workbook wb = Workbook () with NamedTemporaryFile () AS tmp: wb.save (tmp.name) tmp.seek (0) Stream = tmp.read () # load a document, by specifying the attribute template True, the workbook can be saved as a template WB = load_workbook ( ' document.xlsx ' ) wb.template = True wb.save ( ' document_template.xltx ' ) # load a template file, template by specifying the property is False, can save the workbook document to WB = load_workbook ( 'document_template.xltx') wb.template = False wb.save('document.xlsx', as_template=False)
You should monitor and document data attribute extension, in order to save a document in a template, or save the template in the document,
otherwise the result will not open the document table engine
The following situations will save failed
load_workbook = wb ( ' document.xlsx ' ) wb.save ( ' new_document.xlsm ' ) # to be saved as an extension xlsx, or excel can not open the wb = load_workbook ( ' document.xlsm ' ) wb.save ( ' new_document. xlsm ' ) # need to specify the property keep_vba = True, otherwise excel can not open the wb = load_workbook ( ' document.xltm ' , keep_vba = True) wb.save ( ' new_document.xlsm ' ) # If you need a template, you need to specify the extension is * .xltm
Load file
It also could openpyxl.load_workbook () to open a file
from openpyxl Import load_workbook # load the class file to be imported load_workbook WB2 = load_workbook ( ' test.xlsx ' ) Print (wb2.sheetnames) # Output: [ 'Sheet2', 'New Title', 'Sheet1'], the output of the workbook's worksheet first name