python使用openpyxl操做excel

 

def initExcel():
    file_path = "test.xlsx"
    file = load_workbook(file_path)
    table = file["Sheet1"]
#寫入
for i in range(2, 22): table["C" + str(i)] = "123" file.save(file_path)
   # 讀取
  
print(table["E33"].value)
file.close()

讀取公式的計算結果:python

# 重啓應用 安裝win32com,命令爲python -m pip install pypiwin32
from win32com.client import Dispatch

def openExcel(filename):
    xlApp = Dispatch("Excel.Application")
    xlApp.Visible = False
    xlBook = xlApp.Workbooks.Open(filename)
    xlBook.Save()
    xlBook.Close()

openExcel("C:/test.xlsx")
# 必須使用data_only=True,不然打印出來的是公式
file1 = load_workbook(file_path, data_only=True)
table1 = file1["Step 2"]
print(table1["E33"].value)
相關文章
相關標籤/搜索