python 如何删除excel中带有删除线的特定字符串保留没有删除线的单词
注意:经验证 Mac必须安装Microsoft Excel app 但不一定OK
import xlwings as xw
wb = xw.Book('file.xlsx')
ws = wb.sheets('Sheet1')
for cell in ws.range('A1:B3'):
cell_coord = str(cell.address.replace('$', ''))
print('Cell: ' + cell_coord + ', Cell value: ' + str(cell.value), end=', ')
# 判断单元格是否有删除线
st = cell.font.impl.xl.Strikethrough
print('ST value: ' + str(st), end=' ')
if st:
print(', 单元格有删除线字体.')
cell.clear()
# 如果单元格是正常和删除线的混合,则它是None,
elif st is None:
print(', 单元格有混合字体.')
num_chars = len(cell.value)
print(cell_coord + ' Text length: ' + str(num_chars) + ' characters.')
print("个别字符字体有删除线?")
# 检测单元格有混合字体中的带有删除线的个别字符串
char_position = 0
while True:
cur_text_value = cell.characters[char_position].text
if cur_text_value == '':
break
else:
print("'" + cur_text_value + "'", end=' ')
char_is_st_font = cell.characters[char_position].font.impl.xl.Strikethrough
print(char_is_st_font)
if char_is_st_font:
cell.characters[char_position].api.Delete()
else:
char_position += 1
# 否则不存在
else:
print(', 单元格没有删除线字体.')
wb.save('file2.xlsx')
wb.close()