1.相關鏈接:
1)http://www.cnblogs.com/anpengapple/p/6399304.html?utm_source=itdadao&utm_medium=referral (首要推薦)
2)http://openpyxl.readthedocs.io/en/default/usage.html (官網)
3)http://www.jianshu.com/p/642456aa93e2
from openpyxl import load_workbook
wb = load_workbook("template.xlsx")
print(wb.sheetnames) # ['Sheet1', 'Sheet2', 'Sheet3']
sheet = wb.get_sheet_by_name("Sheet3")
print(sheet["C"]) # (<Cell Sheet3.C1>, <Cell Sheet3.C2>, <Cell Sheet3.C3>, <Cell Sheet3.C4>, <Cell Sheet3.C5>, <Cell Sheet3.C6>, <Cell Sheet3.C7>, <Cell Sheet3.C8>, <Cell Sheet3.C9>, <Cell Sheet3.C10>) <-第C列 print(sheet["4"]) # (<Cell Sheet3.A4>, <Cell Sheet3.B4>, <Cell Sheet3.C4>, <Cell Sheet3.D4>, <Cell Sheet3.E4>) <-第4行 print(sheet["C4"].value) # c4 <-第C4格的值 print(sheet.max_row) # 10 <-最大行數 print(sheet.max_column) # 5 <-最大列數 for i in sheet["C"]: print(i.value, end=" ") # c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 <-C列中的所有值
from openpyxl import Workbook wb = Workbook()
sheet = wb.active
sheet.title = "New Shit"
sheet['C3'] = 'Hello world!' for i in range(10): sheet["A%d" % (i+1)].value = i + 1
sheet["E1"].value = "=SUM(A:A)"
wb.save('保存一個新的excel.xlsx')
我在網上查了查,沒有找到什麼相關的介紹,很多網頁都說save的時候會覆蓋原來的文件,但是實際上是不可以的。我也很絕望啊!實在不行,就只能迂迴解決了:先保存成其他的一個文件名,然後把原來的文件刪除,再把新文件改成原來的文件名。