如何从 R 中的 xlsx 文件中检测 "strikethrough" 样式
How to detect "strikethrough" style from xlsx file in R
在 R
中导入 excel 文件时,我必须检查包含“删除线”格式的数据
我们有什么方法可以检测到它们吗?
欢迎使用 R 和 Python 方法
我在下面找到了一个方法:
'#假设1-10列的值为:A,第5个A包含"strikethrough"
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')
但它没有告诉位置(这种情况是行号),当有很多结果时很难知道包含 "strikethrough" 的位置,我如何向量化语句的结果?
R 解
tidyxl
-包可以帮助你...
示例 temp.xlsx,第一个 sheet 的 A1:A4 上有数据。下面是 excel-截图:
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
我在下面展示了一个小示例程序,它使用 openpyxl 包过滤掉应用了删除线的文本(我在版本 2.5.6 和 Python 3.7.0 上对其进行了测试)。抱歉这么久才回复你。
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)
我使用默认工作表在新工作簿上对其进行了测试,A 列的前五行中有字母 a、b、c、d、e,我在其中对 b 和 d 应用了删除线格式。该程序过滤掉 columnA 中已对字体应用删除线的单元格,然后打印剩余单元格、行和值。 col_idx 属性 returns 基于 1 的数字列值。
在 R
中导入 excel 文件时,我必须检查包含“删除线”格式的数据我们有什么方法可以检测到它们吗? 欢迎使用 R 和 Python 方法
我在下面找到了一个方法:
'#假设1-10列的值为:A,第5个A包含"strikethrough"
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')
但它没有告诉位置(这种情况是行号),当有很多结果时很难知道包含 "strikethrough" 的位置,我如何向量化语句的结果?
R 解
tidyxl
-包可以帮助你...
示例 temp.xlsx,第一个 sheet 的 A1:A4 上有数据。下面是 excel-截图:
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
我在下面展示了一个小示例程序,它使用 openpyxl 包过滤掉应用了删除线的文本(我在版本 2.5.6 和 Python 3.7.0 上对其进行了测试)。抱歉这么久才回复你。
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)
我使用默认工作表在新工作簿上对其进行了测试,A 列的前五行中有字母 a、b、c、d、e,我在其中对 b 和 d 应用了删除线格式。该程序过滤掉 columnA 中已对字体应用删除线的单元格,然后打印剩余单元格、行和值。 col_idx 属性 returns 基于 1 的数字列值。