【问题标题】:How to differentiate between two rows of different colours in excel when reading it into pandas dataframe?将excel读入pandas数据框时如何区分两行不同颜色的行?
【发布时间】:2019-06-26 07:29:05
【问题描述】:

我有一个 excel 文件,其中第一行是列名,从第 2 行到第 10 行,行是橙色,从第 11 行到第 25 行,它们是黄色,从第 26 行到第 50 行行它们是绿色的。

现在,橙色对应的行必须在一个数据帧中,黄色对应的行必须在第二个数据帧中,绿色对应的行必须在第三个数据帧中。

现在,我有很多这种类型的 excel 文件,每张纸上的边界行(颜色发生变化)都不同,并且不遵循任何模式,而且我不知道边界行号。

如何通过代码分隔这些行(而不是通过手动查看边界然后将其分开)。

我试图在谷歌上搜索它,但结果是关于格式化数据框并保存它,我想要的是相反的。

【问题讨论】:

标签: python excel pandas dataframe


【解决方案1】:

您可以使用xlrd 包执行此操作。

from xlrd import open_workbook

book = open_workbook("file.xls", formatting_info=True)
sheets = book.sheet_names()
for index, sh in enumerate(sheets):
    sheet = book.sheet_by_index(index)
    rows, cols = sheet.nrows, sheet.ncols
    for row in range(rows):
        for col in range(cols):
            thecell = sheet.cell(row, col)      
            xfx = sheet.cell_xf_index(row, col)
            xf = book.xf_list[xfx]
            bgx = xf.background.pattern_colour_index
            print(bgx)

【讨论】:

  • 这仅适用于 xls 文件。它不适用于 xlsx 文件。 xlsx 文件还有其他解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-08
  • 1970-01-01
  • 2023-02-08
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
相关资源
最近更新 更多