Python如何读取有公式cell的结果内容-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Python如何读取有公式cell的结果内容
    操作Excel通常是用如下三个扩展体:

import xlrd
import xlwt
import openpyxl

创新互联专注于广宗网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供广宗营销型网站建设,广宗网站制作、广宗网页设计、广宗网站官网定制、重庆小程序开发公司服务,打造广宗网络公司原创品牌,更为您提供广宗网站排名全网营销落地服务。

    xlrd(读) 和 xlwt(写)是一对。openpyxl独立,即可读也可写。
    正常读都没有问题,只是读有公式的cell时,会出现如题症状。对此,xlrd似乎没答案,而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()

问题是解决了,速度就是有点慢!


当前题目:Python如何读取有公式cell的结果内容
URL地址:http://scgulin.cn/article/psojji.html