我想用不同颜色的句子上色 - 比如,前半部分用红色,用蓝色休息.
到目前为止,我的代码就像
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import ColorFormat, RGBColor
from pptx.enum.dml import MSO_COLOR_TYPE, MSO_THEME_COLOR
import codecs
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
text_file = open("content.txt", "r")
# read the lyrics file
lines = text_file.readlines()
# page title
title = slide.shapes.title
# text frame in the page
tf = title.textframe
# paragrap in the text frame
p = tf.add_paragraph()
# write the first sentence
#p.text = unicode(lines[0], encoding='utf-8')
p.text = "hello is red the rest is blue"
p.font.bold = True
p.font.color.rgb = RGBColor(255, 0, 0)
prs.save('test.pptx')
text_file.close()
Run Code Online (Sandbox Code Playgroud)
在我的代码中,整个句子都是红色的; 我想知道如何处理不同颜色的不同颜色 - 再说,前半部分用蓝色表示红色.
sca*_*nny 10
将它们添加为单独运行,如下所示:
from pptx.dml.color import RGBColor
from pptx.enum.dml import MSO_THEME_COLOR
from pptx.util import Pt
p = tf.add_paragraph()
run = p.add_run()
run.text = 'hello'
font = run.font
font.name = 'Calibri'
font.size = Pt(18)
font.bold = True
font.color.theme_color = MSO_THEME_COLOR.ACCENT_1
run = p.add_run()
run.text = ' is red and the rest is blue'
run.font.color.rgb = RGBColor(0, 0, 255)
Run Code Online (Sandbox Code Playgroud)
运行是一系列共享相同字符格式的字符.要更改段落中的字符格式,必须使用多次运行.
| 归档时间: |
|
| 查看次数: |
4584 次 |
| 最近记录: |