python針對excel的讀寫操作-----openpyxl
阿新 • • 發佈:2017-12-07
mes val openpyxl rom ges exce 內容 循環 就是
#python讀取excel from openpyxl import load_workbook wb = load_workbook(‘test.xlsx‘) print(wb.sheetnames)
from openpyxl import load_workbook wb = load_workbook(‘test.xlsx‘) sheet = wb.get_sheet_by_name(‘Sheet1‘) print(sheet[‘A‘])#列表A的所有數據
>>>(<Cell ‘Sheet1‘.A1>, <Cell ‘Sheet1‘.A2>, <Cell ‘Sheet1‘.A3>, <Cell ‘Sheet1‘.A4>, <Cell ‘Sheet1‘.A5>, <Cell ‘Sheet1‘.A6>, <Cell ‘Sheet1‘.A7>, <Cell ‘Sheet1‘.A8>, <Cell ‘Sheet1‘.A9>, <Cell ‘Sheet1‘.A10>)
print(sheet[‘4‘])#列表第四行的所有內容
>>>(<Cell ‘Sheet1‘.A4>, <Cell ‘Sheet1‘.B4>, <Cell ‘Sheet1‘.C4>, <Cell ‘Sheet1‘.D4>, <Cell ‘Sheet1‘.E4>)
print(sheet[‘C4‘].value)
>>>哎喲哇
for i in sheet[‘C‘]:
print(i.value)
>>>c1
c2
c3
哎喲哇
c5
c6
c7
c8
c9
c10
#python 寫入excel from openpyxl import Workbook wb = Workbook() sheet = wb.active sheet.title = ‘New sheet‘#定義sheet的標題,下面的sheet名字 sheet[‘C3‘] = ‘hello‘#C列,第三行,寫入hello for i in range(10): sheet[‘A%d‘ %(i+1)].value = i+1#循環遍歷0到9,然後i+1 就是1到10,這句話表示A1-A10,寫入1-10 sheet[‘E1‘].value = ‘=SUM(A:A)‘#在E1寫入A列所有數據的和wb.save(‘保存一個新的excel.xlsx‘)#保存到一個新的excel
python針對excel的讀寫操作-----openpyxl