Ian*_*Ian 7 python excel text-formatting
我得到了一个包含一些文本格式的 excel 文件。有些可以是粗体,有些可以是斜体,有些是 supercase 1,还有一些其他格式(但没有提到的三个那么多)。
例子:
现在,由于这个单元格要作为字典(真实的,人类的,字典)数据库条目,我想保留单元格的格式,因为它有利于告诉这个词的用法(例如粗体中的以上大小写表示单词类型:v(动词)和斜体表示新部分)。
但这一切都在excel单元格中。
当我尝试使用 Toad for Oracle 等数据库工具直接读取 excel 文件时,格式消失了!
<b>v</b>这就是我的工作。我只想知道我们如何在Python中保留或检测excel单元格文本格式。(特别是这三种格式:粗体、斜体和超大写)编辑:
我试图让与xlrd包的文本格式,但我似乎无法找到一种方式来获得文本格式风格为cell对象只包括:ctype,value,和xf_index。它没有关于文本格式的信息,当我使用以下内容创建实例时formatting_info=True:
book = xlrd.open_workbook("HuluHalaDict.xlsx", sys.stdout, 0, xlrd.USE_MMAP, None, None, \
formatting_info=True, on_demand=False, ragged_rows=False)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
NotImplementedError:formatting_info=True 尚未实现
由包xlsx.py文件中的这一行引发xlrd:
if formatting_info:
raise NotImplementedError("formatting_info=True not yet implemented")
Run Code Online (Sandbox Code Playgroud)
我觉得很奇怪,因为我使用的是 0.9.4 xlrd(最新)版本,并且文档说自从 0.6.1 以上的版本以来,格式信息包括在内:
默认格式
默认格式应用于所有空单元格(单元格记录未描述的单元格)。如果可用,首先使用行默认信息(ROW 记录,Rowinfo 类)。如果失败,则使用列默认信息(COLINFO 记录、Colinfo 类)(如果可用)。作为最后的手段,将使用工作表/工作簿默认单元格格式;这应该始终存在于 Excel 文件中,由具有固定索引 15(从 0 开始)的 XF 记录描述。默认情况下,它使用工作表/工作簿默认单元格样式,由第一个 XF 记录(索引 0)描述。xlrd 0.6.1 版中未包含的格式化功能
富文本,即包含部分粗斜体和下划线文本的字符串、字符串内字体的更改等。请参阅 OOo 文档 s3.4 和 s3.2 亚洲语音文本(称为“ruby”),用于日语假名。请参阅 OOo 文档 s3.4.2 (p15) 条件格式。请参阅 OOo 文档 s5.12、s6.21(CONDFMT 记录)、s6.16(CF 记录) 杂项工作表级和书籍级项目,例如打印布局、屏幕窗格。现代 Excel 文件版本不会在文件中保留大部分内置“数字格式”;Excel 根据用户的区域设置加载格式。目前,xlrd 对此的模拟仅限于适用于美国英语语言环境的硬接线表。这可能意味着货币符号、日期顺序、千位分隔符、小数分隔符等不合适。
我在这里做错了吗?我的代码如下所示:
book = xlrd.open_workbook("HuluHalaDict.xlsx", sys.stdout, 0, xlrd.USE_MMAP, None, None, \
formatting_info=True, on_demand=False, ragged_rows=False)
Run Code Online (Sandbox Code Playgroud)
编辑2:
帖子中显示的示例表明它book使用formatting_info=True. 但我在我的实现中检查它。它引发了上面的错误。任何的想法?
我建议你图书馆xlrd https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966
在 GitHub 上https://github.com/python-excel/xlrd
您可以在此处找到有关如何使用 xlrd 确定字体样式的简单示例使用 XLRD 模块和 Python 确定单元格字体样式(斜体与否)
这是一个实际的例子:
from xlrd import open_workbook
path = '/Users/.../Desktop/Workbook1.xls'
wb = open_workbook(path, formatting_info=True)
sheet = wb.sheet_by_name("Sheet1")
cell = sheet.cell(0, 0) # The first cell
print("cell.xf_index is", cell.xf_index)
fmt = wb.xf_list[cell.xf_index]
print("type(fmt) is", type(fmt))
print("Dumped Info:")
fmt.dump()
Run Code Online (Sandbox Code Playgroud)
它输出以下内容:
cell.xf_index is 62
type(fmt) is <class 'xlrd.formatting.XF'>
Dumped Info:
_alignment_flag: 0
_background_flag: 0
_border_flag: 0
_font_flag: 1
_format_flag: 0
_protection_flag: 0
alignment (XFAlignment object):
hor_align: 0
indent_level: 0
rotation: 0
shrink_to_fit: 0
text_direction: 0
text_wrapped: 0
vert_align: 2
background (XFBackground object):
background_colour_index: 65
fill_pattern: 0
pattern_colour_index: 64
border (XFBorder object):
bottom_colour_index: 0
bottom_line_style: 0
diag_colour_index: 0
diag_down: 0
diag_line_style: 0
diag_up: 0
left_colour_index: 0
left_line_style: 0
right_colour_index: 0
right_line_style: 0
top_colour_index: 0
top_line_style: 0
font_index: 6
format_key: 0
is_style: 0
lotus_123_prefix: 0
parent_style_index: 0
protection (XFProtection object):
cell_locked: 1
formula_hidden: 0
xf_index: 62
Run Code Online (Sandbox Code Playgroud)
哪里_font_flag: 1表示粗体
| 归档时间: |
|
| 查看次数: |
17958 次 |
| 最近记录: |