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

如何从xlsx文件中检测删除线样式

  •  1
  • rane  · 技术社区  · 6 年前

    我得检查一下数据 删除线 “在R中导入excel文件时的格式”

    欢迎使用R和Python方法

    3 回复  |  直到 6 年前
        1
  •  3
  •   Wimpel    6 年前

    R-溶液

    这个 tidyxl -包裹可以帮你。。。

    例子测试.xlsx,数据见第一页A1:A4。以下是excel屏幕截图:

    enter image description here

    library(tidyxl)
    
    formats <- xlsx_formats( "temp.xlsx" )
    cells <- xlsx_cells( "temp.xlsx" )
    
    strike <- which( formats$local$font$strike )
    cells[ cells$local_format_id %in% strike, 2 ]
    
    # A tibble: 2 x 1
    #   address
    #   <chr>  
    # 1 A2     
    # 2 A4   
    
        2
  •  2
  •   Jarak    6 年前

    我在下面展示了一个小示例程序,该程序使用openpyxl包(我用python3.7.0在版本2.5.6上测试了它)过滤掉应用了删除线的文本。很抱歉这么久才回复你。

    import openpyxl as opx
    from openpyxl.styles import Font
    
    
    def ignore_strikethrough(cell):
        if cell.font.strike:
            return False
        else:
            return True
    
    
    wb = opx.load_workbook('test.xlsx')
    ws = wb.active
    colA = ws['A']
    fColA = filter(ignore_strikethrough, colA)
    for i in fColA:
        print("Cell {0}{1} has value {2}".format(i.column, i.row, i.value))
        print(i.col_idx)
    

        3
  •  0
  •   rane    6 年前

    我找到了一个方法:

    “#假设从1到10的列有值:A,则第5个A包含“删除线”

    TEST_wb = load_workbook(filename = 'TEST.xlsx')
    TEST_wb_s =  TEST_wb.active
    
    for i in range(1, TEST_wb_s.max_row+1):
        ck_range_A = TEST_wb_s['A'+str(i)] 
        if ck_range_A.font.strikethrough == True:
            print('YES')
        else:
            print('NO') 
    

    但是它不知道位置(这个例子是行号),这很难知道哪里包含“删除线”,当有很多结果时,我如何将语句的结果矢量化?