我有一个带有名称的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)
可悲的是,我现在不知道用熊猫做的方法,不得不自己更多地研究熊猫。 但是您可以使用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对其进行了测试。