首页 > 解决方案 > 如何测试列表是否包含斜体或粗体文本?

问题描述

我有一个带有名称的 exel 文件。有些存储为斜体,有些存储为粗体或正常。斜体名称为“feminin”,男性为普通文本,中性名称为粗体。这份名单很长,大约有 6500 个名字。我希望能够根据它们在 Exel 文件中的存储方式来整理出不同的类型。

喜欢 Print("namn()", 'Female') print(namn(), 'Male')

file_name = 'Namn.xlsx' #xlsx file to open
sheet =  'Sheet1' # name of sheet

import pandas as pan
namn = pan.read_excel(io=file_name, sheet_name=sheet)
print(namn.head(10), 'Female')  # Print 10 first names in exel file

# problem 1. female names are Italics in the original file but gets printed as regular.
# unisex names are in bold and get printed as normal text in the output.
#
# problem 2. How do I sort out Italics names and Bold names stored in the file.

# 10 first names in the exel file

#      Abbe (Normal text in exel file)
#       Abe (Normal text in exel file)
#       Ada (Italics in exel file) 
#      Adam (Normal text in exel file)
#     Adana (Italics in exel file)
#   Adanita (Italics in exel file)
#      Adde (Bold in exel file)
#   Addison (Bold in exel file)
#     Adele (Italics in exel file)
#     Adolf (Normal text in exel file)

标签: python

解决方案


可悲的是,我目前不知道用熊猫做这件事的方法,不得不自己深入研究熊猫。但是你可以使用openpyxl

import openpyxl as xl

file_path = 'absolute/path/to/your.xlsx'

wb = xl.load_workbook(file_path)
ws = wb.active 

#changing to other worksheets:
#ws = wb["Title of the Worksheet"]

a1 = ws['A1']
print(a1.font)# lists the params of font ( i for italic ) 
print(a1.font.i == True)

刚刚使用包含单个工作表和 A1 中的斜体字符串的 .xlsx 对其进行了测试。


推荐阅读