如何使用Python从Excel文件中提取单元格格式(粗体,斜体,...)? [英] How to extract cell format (bold, italic, ...) from an Excel file using Python?
问题描述
我正在尝试从Excel文件中提取单元格(基本上是文本)的内容和文本格式.我正在处理的Excel如下图所示:
I'm trying to extract from an Excel file the content of a cell (essentially text) and the format of the text. The Excel I am dealing with looks like the screenshot below :
单元格中的文本可能为粗体,斜体或中风,我需要将文本和格式提取为python字符串.例如,如果一个单元格包含:
Texts in cells may be bold, italic or stroke and I need to extract the text and the format into a python string. For example, if a cell contains :
- 第1行
- 第2行
- 第3行
- line 1
- line 2
- line 3
我想获得一个像这样的字符串Python:
I would like to obtain a string Python that's look like :
- line 1 /n- **line 2**/n- *line 3**
以获取文本和格式信息.
in order to get the text and the format information.
我尝试使用openpyxl找到解决方案,但似乎只能应用而不提取格式单元格.库xlrd似乎不适合xlsx.我目前正在尝试使用pyexcel库.
I've tried to find a solution using openpyxl, but it seems that it is only possible to apply and not extract format cell. Library xlrd seems not fitted for xlsx. I am currently trying with pyexcel library.
你有什么主意吗?谢谢.
Do you have any idea ? Thanks.
推荐答案
您可以从openpyxl导入Font,并使用cell.font.bold
检查单元格是否以粗体显示,它给出的是True还是False.
You can import Font from openpyxl and check if a cell is written in bold by using cell.font.bold
, it gives a True or False.
cell=sheet[A2]
bold_status=cell.font.bold
italic_status=cell.font.italic
有关openpyxl中字体的更多信息: http://openpyxl.readthedocs.io/en/2.5/api/openpyxl.styles.fonts.html
More info on the fonts in openpyxl: http://openpyxl.readthedocs.io/en/2.5/api/openpyxl.styles.fonts.html
这篇关于如何使用Python从Excel文件中提取单元格格式(粗体,斜体,...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!