我想使用python-pptx模块更改给定powerpoint演示文稿中每个包含文本的形状的校对语言。 不幸的是我没有管理。 :(
我正在使用Python 3.6.3和python-pptx 0.6.7。
我的代码如下所示:
from pptx import Presentation
from pptx.enum.lang import MSO_LANGUAGE_ID
# In this example code, all proofing language is set to ENGLISH_UK
# all languages can be found in the docs for python-pptx
new_language = MSO_LANGUAGE_ID.ENGLISH_UK
input_file = 'test_pptx.pptx'
output_file = input_file[:-5] + '_modified.pptx'
# Open the presentation
prs = Presentation(input_file)
# iterate through all slides
for slide_no, slide in enumerate(prs.slides):
# iterate through all shapes/objects on one slide
for shape in slide.shapes:
# check if the shape/object has text (pictures e.g. don't have text)
if shape.has_text_frame:
# print some output to the console for now
print('SLIDE NO# ', slide_no + 1)
print('Object-Name: ', shape.name)
print('Text -->', shape.text)
# check for each paragraph of text for the actual shape/object
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
# display the current language
print('Actual set language: ', run.font.language_id)
# set the 'new_language'
run.font.language_id = new_language
else:
print('SLIDE NO# ', slide_no + 1, ': This object "', shape.name, '" has no text.')
print(' +++++ next element +++++ ')
print('--------- next slide ---------')
# save pptx with new filename
prs.save(output_file)
此代码现在可以使用! (再次感谢史蒂夫!)
请帮忙!提前谢谢!
答案 0 :(得分:0)
我实际上并不完全确定校对功能决定使用哪种字典的所有规则,但是语言是在运行级别设置的,我认为这是一个很好的起点。
这有一定的意义,因为你可以在一段文字中间有一个外来短语,只有一个形状级别的语言设置不支持。
一旦超过for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
font = run.font
print(font.language_id)
测试,您需要一些额外的代码:
TURKISH (1055)
ENGLISH_UK (2057)
...
这应该给你类似的东西:
.xml_value
请注意,语言ID值通过 for run in paragraph.runs:
font = run.font
language_id = font.language_id
print('\'%s\'' % run.text, language_id, language_id.xml_value)
属性上提供的标准语言代码提供了一些附加信息,因此您可以将输出详细说明为:
'the rain in ' ENGLISH_US (1033) en-US
'España' SPANISH (1034) es-ES_tradnl
...
得到类似的东西:
{{1}}