如何在python中使用xlrd检查单元格是否有公式?

时间:2014-10-06 10:01:43

标签: python python-2.7 python-3.x xlrd

我想从excel工作簿访问具有公式的单元格。我有一个工作正常的脚本,但只读取excel中的数据。

我只需要用公式打印细胞。一个例子将非常感激。

1 个答案:

答案 0 :(得分:0)

以下功能怎么样: - 输入参数需要传递如下的函数: -

Input Parameter:- validateExcelData("Desktop/test.xls,55")

注意: - 这里55是使用公式的计数,对于下面的情况,如果条件不起作用,你将得到结果失败,但如果整数值没有公式,那么条件将起作用结果你会得到通过。这也不是有效的代码。看看它是否有帮助:)

def validateExcelData(input):
    inputParam=input.split(",")
    filePath=inputParam[0].strip()
    value=inputParam[1].strip()
    outputParams="Fail"
    try:
        fs = os.sep.join((os.path.expanduser("~"),filePath))
        wb=xlrd.open_workbook(fs)
        for s in wb.sheets():
            for row in range(s.nrows):
                for col in range(s.ncols):
                    if s.cell(row,col).value==int(value):
                        outputParams ="PASS"
                        break
    except:
        outputParams = GetException()


    return outputParams