【问题标题】:How can I read colored rows of an xlsx file via python?如何通过 python 读取 xlsx 文件的彩色行?
【发布时间】:2021-08-09 07:59:51
【问题描述】:

数据:

1  2  3
4  5  6
7  8  9 
10 11 12
13 14 15
16 17 18

这是彩色数据

如何通过 python 读取 xlsx 文件的彩色行?

我想看到的输出:

 4  5  6
10 11 12

【问题讨论】:

  • 你是如何在 xlsx 文件中制作颜色的?你能分享你的 xlsx 文件(或者类似的模板)吗?

标签: python pandas dataframe xlsx


【解决方案1】:

您可以使用 xlrd 但仅支持 xls 格式。当您使用此代码时,您将看到these indexed numbersfrom that kind of format,您可以从IndexColor Class page查看这些索引

import xlrd
import numpy as np
import pandas as pd
wb = xlrd.open_workbook('color_codes.xls', formatting_info=True)
sheet = wb.sheet_by_name("Sheet1")
bgcol=np.zeros([sheet.nrows,sheet.ncols])
for row in range(sheet.nrows):
    for col in range(sheet.ncols):
        c = sheet.cell(row, col)
        cif = sheet.cell_xf_index(row, col)
        iif = wb.xf_list[cif]
        cbg = iif.background.pattern_colour_index
        bgcol[row,col] = cbg
        colormask=pd.DataFrame(bgcol)
print(colormask)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 2017-10-26
    相关资源
    最近更新 更多