Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python pptx模块中的文本颜色_Python_Powerpoint - Fatal编程技术网

python pptx模块中的文本颜色

python pptx模块中的文本颜色,python,powerpoint,Python,Powerpoint,我想给一个句子涂上不同的颜色——比如说,前半部分是红色,其余部分是蓝色 到目前为止,我的代码是 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 = Presentati

我想给一个句子涂上不同的颜色——比如说,前半部分是红色,其余部分是蓝色

到目前为止,我的代码是

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()

在我的代码中,整个句子是红色的;我想知道如何将不同的单词表示为不同的颜色-同样,前半部分是红色,其余部分是蓝色。

将它们作为单独的运行添加,如下所示:

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.text.text_frame._set_font(font,size,bold,italic)

哇!你还可以,在你的代码中添加一个如何改变字体(比如说前半部分使用不同的字体)?我很感激。我想这方面的文档非常好,你可以在这里找到:。但我会补充一点:)@scanny:你能回答这个问题吗: