Python:openpyxl将字体更改为粗体 [英] Python: openpyxl change font to bold

查看:1275
本文介绍了Python:openpyxl将字体更改为粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows上的Python版本3.6和最新版本的openxlpy模块(v2.4.8).

I'm using Python version 3.6 and the latest version of the openxlpy module (v2.4.8) on Windows.

我想将某个单元格中的某些字体更改为粗体,但是我不希望该单元格中包含的所有文本都以粗体显示.简而言之,我将数据保存到使用openxlpy创建的新Excel工作簿中.我在一个单元格中保存了多行数据.我只希望每个单元格的第一行都加粗.

I want to change certain font to bold in a cell, but I don't want all the text contained in the cell to be bold. In short, I'm saving data to a new Excel workbook that I've created using openxlpy. I'm saving multiple lines of data in one cell. I only want the first line of each cell to be bold.

我已经在openpyxl文档中和在线上进行了无处不在的搜索,但是找不到任何东西.在我看来,您只能将字体样式应用于似乎不合适的整个单元格.在Microsoft Excel中,您可以将不同的字体样式应用于一个单元格中的不同数据.

I've searched everywhere in the openpyxl documentation and online but I can't find anything. It appears to me that you can only apply font styling to the entire cell which doesn't seem right. In Microsoft Excel you can apply different font styles to different data within one cell.

总而言之,我只想将单元格中的某些文本加粗,而不要将单元格的全部内容加粗.

In summary, I want to only bold certain text in a cell and not bold the entire contents of the cell.

推荐答案

回答帖子标题,但不回答具体问题.

Answers post title but not ops specific question.

from openpyxl.workbook import Workbook
from openpyxl.styles import Font
wb = Workbook()
ws = wb.active
ws['B3'] = "Hello"
ws['B3'].font = Font(bold=True)
wb.save("BoldDemo.xlsx")

这篇关于Python:openpyxl将字体更改为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆