python - 如何测试列表是否包含斜体或粗体文本?
问题描述
我有一个带有名称的 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 对其进行了测试。
推荐阅读
- json-api - 如何将嵌套关系与 JSON API 中的 POST 的属性相关联
- android - 使用 React-Native 重新加载无法在 android 上运行
- php - 为什么 PHP 将 << (double less than) 视为 strip_tags 中的标签?
- cordova - ionic 3 - 应用程序关闭时如何接收本地通知
- r - Dcast 功能 -> 重新排序列
- c++ - C++11:发布者/消费者模式不会完成,除非发布者休眠
- android - 改造android标头用户代理未显示在日志上
- sonarqube - Sonarqube 用于 PLSQL 脚本中的编译错误
- php - 如何显示 WP_Mail 错误?
- python - groovy.json.JsonException:如何解析包含另一个 JSON 字符串的 JSON 字符串?