Python 设置字体
时间: 2023-11-05 12:53:21 浏览: 137
Python中可以使用matplotlib和python-docx库来设置字体。对于matplotlib库,可以使用以下方法来设置字体:
方法一:
```python
import matplotlib
font = {'family' : 'Microsoft YaHei', 'weight' : 'bold', 'size' : 'larger'}
matplotlib.rc("font",**font)
```
方法二:
```python
import matplotlib
matplotlib.rc("font",family='Microsoft YaHei',weight="bold")
```
方法三:
```python
from matplotlib import font_manager
window系统下的字体路径
# 宋体:fname="C:\Windows\Fonts\simsun.ttc"
# Times New Roman:fname="C:\Windows\Fonts\times.ttf
my_font=font_manager.FontProperties(fname="C:\Windows\Fonts\simsun.ttc")
# 在需要中文显示的地方添加fontproperties=my_font即可
plt.xticks(list(x)[::3],_xtick_lables[::3],rotation=45,fontproperties=my_font)
```
对于python-docx库,可以使用以下方法来设置字体:
方法一:
```python
word = word.styles['Normal']
word.font.name = '仿宋'
word._element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋')
word.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
```
方法二:
```python
Title = word.styles['Title']
Title.font.name = '仿宋'
Title.element.rPr.rFonts.set(qn('w:eastAsia'),'仿宋')
Title.font.size = Pt(22)
Title.font.color.rgb = RGBColor(0,0,0)
Title.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
```
阅读全文