python - 如何在单独的运行中更改字体
问题描述
我尝试在单次运行中更改格式,而不使用根据文档https://python-docx.readthedocs.io/en/latest/user/text.html的样式
我不知道为什么,但下面的例子并没有改变它。
import os, sqlite3
import tkinter as tk
from tkinter import ttk, Menu
from docx import Document
from docx.shared import Pt
def create_offer():
offer = Document()
p = offer.add_paragraph("Just a paragraph")
run = offer.add_paragraph("Test run").add_run()
font = run.font
font.name = 'Calibri'
font.bold = True
font.size = Pt(12)
run = offer.add_paragraph("2nd test run").add_run()
offer.save("Demo.docx")
提前致谢
解决方案
您应该将要格式化的文本添加为add_run()
函数中的参数,而不是add_paragraph()
函数。
然后你的create_offer
函数看起来像
from docx import Document
from docx.shared import Pt
def create_offer():
offer = Document()
p = offer.add_paragraph("Just a paragraph")
run = offer.add_paragraph().add_run("Test run")
font = run.font
font.name = 'Calibri'
font.bold = True
font.size = Pt(12)
run = offer.add_paragraph().add_run("2nd test run")
offer.save("Demo.docx")
推荐阅读
- git - 远程:匿名访问
/ .git 拒绝。致命:'https://github.com/ 的身份验证失败 / .git/' - android - 如何在滚动时使用回收器视图识别部分适配器的子项的位置
- android - 科尔多瓦构建 android - aapt 超时
- python - 预期缩进块错误(django)
- javascript - 使用 jQuery、Ajax 和 PHP 的动态依赖选择框
- java - 使用java从文本中删除url的问题
- python - 使用python在Excel中查找具有不同位置的重复值
- c# - 如何正确获取自定义属性中的数据库值以验证 IdentityRole?
- image-processing - 使用 YOLO 进行车牌检测
- python - 为什么程序运行时错误?