Openpyxl - 使用条件格式

时间:2018-01-09 12:37:12

标签: python excel openpyxl conditional-formatting

第一次在这里提问。我正在学习Python,我遇到了上一段代码的问题。作为参考,我使用的是3.6版本,所有内容都是在过去几周安装的,所以应该更新。

当我尝试使用Openpyxl打开.xlsm文件时,我会在下面粘贴错误。从工作簿中删除条件格式后,一切都按预期工作。它在使用Pandas时也有效。

我之前使用过该模块和其他文件没有问题。我发现了一些类似的问题但通常更老,据说在较旧的补丁中得到纠正。代码片段应该只构建文件路径并与其他文件一起使用。这有什么亮点吗?

    import openpyxl

    Ano = '-2018'
    arquivo = 'Controle de Produção v2 '
    arquivo = arquivo + dataEx[3:]+ '-' + Ano[3:] 
    wb = openpyxl.load_workbook("C:\\Users\\Desktop\\Dropbox\\Produção Equipes\\"+arquivo+".xlsx")
Traceback (most recent call last):   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\descriptors\base.py", line 57, in _convert
    value = expected_type(value) ValueError: could not convert string to float: '$G$16-0.015'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):   File "C:\Users\Desktop\Documents\Hugo\Python\Projetos\Produtividade\Produtividade.py", line 33, in <module>
    wb = openpyxl.load_workbook("C:\\Users\\Desktop\\Dropbox\\Produção Equipes\\"+arquivo+".xlsx", False, True) #DEFINIR CAMINHO COMPLETO   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\reader\excel.py", line 245, in load_workbook
    ws_parser.parse()   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\reader\worksheet.py", line 127, in parse
    dispatcher[tag_name](element)   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\reader\worksheet.py", line 286, in parser_conditional_formatting
    cf = ConditionalFormatting.from_tree(element)   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\descriptors\serialisable.py", line 79, in from_tree
    obj = desc.expected_type.from_tree(el)   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\descriptors\serialisable.py", line 79, in from_tree
    obj = desc.expected_type.from_tree(el)   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\descriptors\serialisable.py", line 79, in from_tree
    obj = desc.expected_type.from_tree(el)   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\descriptors\serialisable.py", line 92, in from_tree
    return cls(**attrib)   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\formatting\rule.py", line 60, in __init__
    self.val = val   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\formatting\rule.py", line 39, in __set__
    super(ValueDescriptor, self).__set__(instance, value)   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\descriptors\base.py", line 69, in __set__
    value = _convert(self.expected_type, value)   File "C:\Users\Desktop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\descriptors\base.py", line 59, in _convert
    raise TypeError('expected ' + str(expected_type)) TypeError: expected <class 'float'>

1 个答案:

答案 0 :(得分:0)

嗯,不是一个明确的答案,但该程序能够在评论这些内容后完全运行:

def parser_conditional_formatting(self, element):
          None #CHANGED FOR PRODUTIVIDADE.PY
##        cf = ConditionalFormatting.from_tree(element)
##        for rule in cf.rules:
##            if rule.dxfId is not None:
##                rule.dxf = self.differential_styles[rule.dxfId]
##            self.ws.conditional_formatting.add(cf.sqref, rule)

openpyxl \ reader \ worksheet.py

我知道这不是一个真正的解决方案,但至少程序正在加载工作簿。我将尝试在BitBucket中提交一个问题,以便进行更好的分析。