代码之家  ›  专栏  ›  技术社区  ›  Manvi

Python读取excel工作表(xlsx)中的单元格,并带有一些背景色

  •  0
  • Manvi  · 技术社区  · 7 年前

    我正在尝试阅读excel工作表(xlsx),它使用背景色来区分值。

    我尝试了以下库:

    1. 熊猫没有找到任何选项来读取基于背景色的单元格。
    2. xlrd。

      import xlrd
      xlrd.open_workbook("filename.xlsx", formatting_info=True)
      

    它给出的错误如下: NotImplementedError:formatting\u info=True尚未实现。

    1. StyleFrame(正如DeepSpace在《基于excel工作表中的单元格颜色和文本颜色对数据框进行子集设置》中所建议的 )

      from StyleFrame import StyleFrame, utils
      sf = StyleFrame.read_excel('filename.xlsx', read_style=True, use_openpyxl_styles=False)
      

    它给出的错误如下:

    Traceback (most recent call last):
    File "proj_path/read_excel.py", line 22, in <module>
    sf = StyleFrame.read_excel('filename.xlsx', read_style=True, use_openpyxl_styles=False)
    File "C:\Anaconda\lib\site-packages\StyleFrame\deprecations.py", line 22, in inner
    return func(*args, **kwargs)
    File "C:\Anaconda\lib\site-packages\StyleFrame\style_frame.py", line 220, in read_excel
    _read_style()
    File "C:\Anaconda\lib\site-packages\StyleFrame\style_frame.py", line 209, in _read_style
    read_comments and current_cell.comment)
    File "C:\Anaconda\lib\site-packages\StyleFrame\styler.py", line 127, in from_openpyxl_style
    font_color = theme_colors[openpyxl_style.font.color.theme]
    TypeError: list indices must be integers or slices, not Integer
    

    任何帮助我朝着正确方向发展的建议都将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Juan C    7 年前

    尽管可以通过Python实现,但最简单的方法应该是在Excel中按颜色过滤,复制该表并将其粘贴到其他位置,然后像导入任何带有Pandas的Excel文件一样导入它。

    正如DeepSpace所评论的那样,它以前是通过Python完成的,但这相当麻烦