說明:本文檔內容參考自 https://www.cnblogs.com/zeke-python-road/p/8986318.html (作者:關關雎鳩`)的文檔
1 from openpyxl import Workbook 2 3 from openpyxl import load_workbook 4 # 實例化一個操作對象 5 wb = Workbook() 6 7 # 獲取當前活躍狀態的sheet 8 ws = wb.active 9 10 ''' 11 # 基本操作,插入方式按順序逐行插入 12 # 單元格內容控制 13 ws['A1'] = '姓名' 14 ws['B1'] = '性別' 15 # 在上文基礎上添加整行(上文存在三行,則從第四行開始整行插入) 16 ws.append(['項目','姓名','時間','報價','備注']) 17 ws.append([1,2,3]) 18 ws.append([23,34,45,56]) 19 # 單元格內容控制 20 ws['A2'] = 'jony' 21 ws['A3'] = 'male' 22 # 文件保存(必須用絕對路徑) 23 wb.save('/home/ht/fir.xlsx') 24 ''' 25 26 ''' 27 # 創建新的工作區,並輸入該區的標簽名(標簽名,位置)* 位置:該區在標簽中的排序 28 w1 = wb.create_sheet('sheet1') 29 w2 = wb.create_sheet('sheet2') 30 w3 = wb.create_sheet('sheet3',0) 31 32 # 這里是修改該區標簽名 33 w1.title = 'sheet-1' 34 w2.title = 'sheet-2' 35 w3.title = 'sheet-3' 36 37 w2.append(['ds','hp','wq']) 38 39 # 設置標簽的背景色(不是表格的單元格) 40 w2.sheet_properties.tabColor = "1072BA" 41 42 # 可以通過工作區的title名來獲取該sheet對象 43 wanna = wb.get_sheet_by_name('sheet-2') 44 45 print(wanna) 46 print(wb.sheetnames) 47 48 # <Worksheet "sheet-2"> 49 # ['sheet-3', 'Sheet', 'sheet-1', 'sheet-2'] 50 51 wb['sheet-3'].append([1,2,3,4,5]) 52 53 # 復制工作區,新的工作區的默認命名為sheet-3 Copy 54 new_3 = wb.copy_worksheet(w3) 55 # 復制品重命名 56 new_3.title = 'new' 57 wb.save('/home/ht/mul_sheet.xlsx') 58 ''' 59 60 ''' 61 sh1 = wb.active 62 sh2 = wb.create_sheet('sheet-cell') 63 64 # 單表格坐標數值輸入 65 sh2['A1'] = 'aaa插入內容' 66 # 單元格坐標接受小寫 67 sh2['d4'] = '表格小寫' 68 # 單元格行列值坐標輸入 69 cell1 = sh2.cell(row=3,column=2,value='三行二列') 70 cell2 = sh2.cell(3,4,'三行四列') 71 print(cell1.value) 72 # 三行二列 73 wb.save('/home/ht/sheet-cell.xlsx') 74 ''' 75 76 ''' 77 # 批量獲取單元格 78 mul_cell = wb.active 79 mul_cell.append(['a1','b1','c1','d1']) 80 mul_cell.append(['a2','b2','c2','d2']) 81 mul_cell.append(['a3','b3','c3','d3']) 82 83 # 獲取A列所有單元格 84 print(mul_cell['a']) 85 # (<Cell 'Sheet'.A1>, 86 # <Cell 'Sheet'.A2>, 87 # <Cell 'Sheet'.A3>) 88 89 # 獲取 BCD 三列所有單元格 90 print(mul_cell['b:d']) 91 # ((<Cell 'Sheet'.B1>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.B3>), 92 # (<Cell 'Sheet'.C1>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.C3>), 93 # (<Cell 'Sheet'.D1>, <Cell 'Sheet'.D2>, <Cell 'Sheet'.D3>)) 94 95 # 獲取第 2到3 行所有單元格 96 print(mul_cell[2:3]) 97 # ((<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.D2>), 98 # (<Cell 'Sheet'.A3>, <Cell 'Sheet'.B3>, <Cell 'Sheet'.C3>, <Cell 'Sheet'.D3>)) 99 100 # iter_rows 方法,設定參數,獲取二行三列區塊內的所有單元格(獲取基本單位為行,再從行內獲取單元格) 101 for r in mul_cell.iter_rows(min_row=1,max_row=2,min_col=1,max_col=3): 102 print(r) 103 for c in r: 104 print(c,c.value) 105 106 # (<Cell 'Sheet'.A1>, <Cell 'Sheet'.B1>, <Cell 'Sheet'.C1>) 107 # <Cell 'Sheet'.A1> a1 108 # <Cell 'Sheet'.B1> b1 109 # <Cell 'Sheet'.C1> c1 110 # (<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>) 111 # <Cell 'Sheet'.A2> a2 112 # <Cell 'Sheet'.B2> b2 113 # <Cell 'Sheet'.C2> c2 114 115 # iter_rows 方法,設定參數,獲取二行三列區塊內的所有單元格(獲取基本單位為列,再從列內獲取單元格) 116 for r in mul_cell.iter_cols(min_row=1,max_row=2,min_col=1,max_col=3): 117 print(r) 118 119 # (<Cell 'Sheet'.A1>, <Cell 'Sheet'.A2>) 120 # (<Cell 'Sheet'.B1>, <Cell 'Sheet'.B2>) 121 # (<Cell 'Sheet'.C1>, <Cell 'Sheet'.C2>) 122 123 # 獲取所有行 124 for r in mul_cell.rows: 125 print(r) 126 # (<Cell 'Sheet'.A1>, <Cell 'Sheet'.B1>, <Cell 'Sheet'.C1>, <Cell 'Sheet'.D1>) 127 # (<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.D2>) 128 # (<Cell 'Sheet'.A3>, <Cell 'Sheet'.B3>, <Cell 'Sheet'.C3>, <Cell 'Sheet'.D3>) 129 130 print(mul_cell.rows) 131 # <generator object Worksheet._cells_by_row at 0x7f4c615312b0> 132 133 # 獲取所有列 134 print(mul_cell.columns) 135 # <generator object Worksheet._cells_by_col at 0x7f4c615312b0> 136 wb.save('/home/ht/mul_cell.xlsx') 137 ''' 138 139 ''' 140 from openpyxl import load_workbook 141 142 # 操作文件 143 read_sheet = load_workbook('/home/ht/mul_sheet.xlsx') 144 145 read_sheet.guess_types = True 146 147 current = read_sheet.active 148 current['e2'] = '65%' 149 150 read_sheet.save('/home/ht/mul_sheet.xlsx') 151 ''' 152 153 ''' 154 from openpyxl import load_workbook 155 wsheet = load_workbook('/home/ht/mul_cell.xlsx') 156 157 # sheet 對象不存在腳標,只能通過坐標獲取,wsheet.active.rows[1]是不能獲取第二行的! 158 print(wsheet.active['a'][1].value) 159 print(wsheet.active['2'][1].value) 160 # a2 161 # b2 162 ''' 163 164 ''' 單元格分割合並 165 166 deal_cell = load_workbook('/home/ht/sum_sheet.xlsx') 167 sheet = deal_cell.active 168 169 # 合並第一行1到4單元格(a1,b1,c1,d1) 170 sheet.merge_cells('a1:d1') 171 172 # 將 第一行 a1 到 d1 位置的單元格分離出來 173 sheet.unmerge_cells('a1:d1') 174 175 # 采用區塊的方式合並或分割操作 176 sheet.merge_cells(start_row=1,end_row=3,start_column=4,end_column=6) 177 178 deal_cell.save('/home/ht/sum_sheet.xlsx') 179 ''' 180 181 ''' 單元格插入圖片 182 183 # 需要安裝PIL圖片包 184 from openpyxl.drawing.image import Image 185 186 img = Image('/home/ht/qcode.png') 187 ws.add_image(img,'F3') # 這里圖片填充的位置坐標必須使用大寫字母 188 wb.save('/home/ht/addimg.xlsx') 189 '''