1.1 openpyxl basic use
1, openpyxl be read as json format xlsx
# ! / Usr / bin / the env Python # - * - Coding: UTF-. 8 - * - # - * - Coding: UTF-. 8 - * - Import JSON from openpyxl Import load_workbook DEF read_xlsx_to_json (file_home): WB = load_workbook (filename = file_home) sheet_ranges = WB [ ' Sheet1 ' ] WS = WB [ ' Sheet1 ' ] rows = ws.rows # obtain forms all rows and columns, both of which are iterative thead = [col.value for COL in rows. the Next ()] #The first line of the table as the Key Data = [] for Row in rows: Line = [col.value for COL in Row] tmp_dic = {} for index, Val in the enumerate (thead): tmp_dic [Val] = Line [index] data.append (tmp_dic) return json.dumps (Data, ensure_ascii = False, indent =. 4 ) # Print read_xlsx_to_json ( 'dd.xlsx') '' ' [ { "employee ID": null, "department name": "HLT Group ", " phone number ":1393999934, " role ": null, "Immediate supervisor": null, "Position": "CEO", "micro enterprise that uniquely identifies the letter": null, "relevant departments": null, "name": "Wang five", "E-mail": "[email protected] ", " functions ": null, " gender ":" male " }, { " employee number ": null, " department name ":" government division ", " phone number ": 61616116161616, " role ": null, " immediate supervisor ":" Wang five ", " position ":" Chief Operating Officer ", " micro enterprises believe that uniquely identifies ": null, " relevant departments ": null, "Name": "John Doe", "E-mail": "[email protected]", "functions": null, "gender": "female" } ] '' '
2, openpyxl create a xlsx file
# ! / Usr / bin / env Python # - * - Coding: UTF-8 - * - Import json from openpyxl Import Workbook # create a xlsx file DEF create_xlsx (DATA_LIST): wb = Workbook () # Create a workbook ws = wb .active # activate the worksheet ws1 = wb.create_sheet ( " MySheet " ) # create mysheet table ws.title = " New the Title " # indicated that the Title was changed to New ws.sheet_properties.tabColor = " 1072BA " # color for row_index, Row in the enumerate (DATA_LIST): thead_list = DATA_LIST [0] # [ 'ID', 'name', 'Sex', 'Age'] row_index + = 1 # Excel rows starting from 1 calculated for the Column_Index, thead in the enumerate (thead_list): the Column_Index +. 1 = # Excel rows is calculated from the start. 1 IF row_index. 1 ==: # Step: filling a first data row, the first table (ID Sex Age name) D = ws.cell (Row =. 1, column = the Column_Index, value = thead) the else : # Step: New table data d = ws.cell(row=row_index, column=column_index, value=row[column_index-1]) wb.save('test.xlsx')#保存 data_list = [ ['id','name','sex','age'], ['1','张三','男','18'], ['2', ' John Doe ' , ' male ' , ' 19 ' ], [ ' 3 ' , ' king five ' , ' female ' , ' 20 ' ], ] create_xlsx (DATA_LIST)
3, openpyxl modify the value of the tenth column
# ! / Usr / bin / Python the env # - * - Coding: UTF-. 8 - * - Import JSON from openpyxl Import load_workbook # The values in the table to modify the tenth row uniform 'new new Val' DEF change_xlsx (file_home): WB load_workbook = (filename = file_home) sheet_ranges = WB [ ' Sheet1 ' ] WS = WB [ ' Sheet1 ' ] rows = ws.rows # obtain forms all rows and columns, both of which are iterative for row, line in the enumerate ( rows): IF Row =! 0: row += 1 ws.cell(row=row, column=10).value = 'new val' wb.save('f5.xlsx') change_xlsx('dd.xlsx')
# ! / Usr / bin / Python the env # - * - Coding: UTF-. 8 - * - from openpyxl Import load_workbook file_home = ' f5.xlsx ' WB = load_workbook (filename = file_home) sheet_ranges = WB [ ' Sheet1 ' ] WS = WB [ ' Sheet1 ' ] # of the second row, second column value is modified to 'modified value' ws.cell (row = 2, column = 2) .Value = ' modified value ' wb.save ( ' f6.xlsx ' )
4, adding new content file xlsx later (in the order of data insertion)
# ! / Usr / bin / the env Python # - * - Coding: UTF-. 8 - * - Import JSON, SYS from openpyxl Import load_workbook # in xlsx file add the new data is inserted into xlsx file corresponding to the location in order DEF append_xlsx (file_home, DATA_LIST): '' ' : param file_home: XLSX file path : param data_list: inserting the data ' '' WB = load_workbook (filename = file_home) sheet_ranges = WB [ ' Sheet1 ' ] WS = WB [ ' Sheet1 ' ] Print DATA_LIST ws.append (DATA_LIST) wb.save ( ' f7.xlsx ' ) DATA_LIST = [ ' mailbox ' , '' , ' phone number ' , ' Sex ' ] append_xlsx ( ' dd.xlsx ' , DATA_LIST)