Python 讀取有公式cell的結果內容例項方法
阿新 • • 發佈:2020-02-17
操作Excel通常是用如下三個擴充套件體:
- import xlrd
- import xlwt
- import openpyxl
wb1 = openpyxl.load_workbook(xlsxFileWithFullPath,data_only=True)
就是加上 “data_only=True” 這個引數。
xlsxFileWithFullPath ---要操作的檔案。
加上以後你會發現,還是依然如故,或者是時可時否!
如把檔案開啟,再儲存一遍,執行程式,第一遍可以,第二遍就不行了!
其實, 關於 data_only=True 這個引數有個重要說明:
# data_only (bool) – controls whether cells with formula have either the formula (default) or the value stored the last time Excel read the sheet
這就解釋了上述時可時否的問題。
這樣解決問題的辦法就有了:用程式來完成那個儲存檔案的任務即可!
(1)
def ReadLine(self,tip1,tip2,movingRC,fixedRC,RorC,totalCells,sheetName,xlsxFileWithFullPath):
[!!]just open and save the file once! why? see bellow!
xlsxDealer.`JustOpenAndSaveTheFile`(xlsxFileWithFullPath)
(2)
def JustOpenAndSaveTheFile(self,file_name):
from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible = False
xlBook = xlApp.Workbooks.Open(file_name)
xlBook.Save()
xlBook.Close()
問題是解決了,速度就是有點慢!
以上就是本次介紹的全部相關知識點,感謝大家的學習,歡迎轉載。