seo 专业为网站建设专业网站开发方案
seo 专业为网站建设,专业网站开发方案,wordpress登录才能看内容,做网站年入多少目录 一、修改PPT中每一页的字体二、将文本框中的字都放到word里 将一份PPT的每一页字体、大小、是否加粗都统一#xff0c;是一个常见需求。特别是字体统一是高频、热点需求。在python操控PPT常用库python-pptx中有一个bug#xff0c;对字体的修改只能修改数字和英文字母是一个常见需求。特别是字体统一是高频、热点需求。在python操控PPT常用库python-pptx中有一个bug对字体的修改只能修改数字和英文字母无法修改汉字。即
run.font.namet属性只能修改英文和数字并且
run.font.name识别的也是英文和数字的名称。如文本框中英文和数字是’Arial’汉字是宋体则会返回’Arial’。因为这个包没有针对汉字的API而且这个包很久没更新了开发者提供了解决思路是修改office文件的底层xml来实现修改xml中的a:ea的typeface属性网上已经有人用 pptx_ea_font 这个包实现了该功能。 首先安装对应的包 pptx和docx的包为注意不是pptx和docx
pip install python-pptx
pip install python-docxpptx_ea_font 安装方法为
pip install pptx_ea_font 导入相应模块
from pptx import Presentation
import pptx_ea_font
from docx import Document
from pptx.util import Cm, Pt一、修改PPT中每一页的字体
1、可以修改字体、大小、是否加粗 2、图形、图表、表格的汉字还不能修改需要下一步增加该功能
函数如下
#修改字体类型和大小
def change_ppt_font(ppt_file, new_font,new_sizeNone,boldNone):# 打开PPT文件presentation Presentation(ppt_file)# 循环遍历每个slidefor slide in presentation.slides:# 循环遍历slide中的每个shapefor shape in slide.shapes:# 检查shape类型是否为文本框if shape.has_text_frame:# 获取文本框中的文字text_frame shape.text_framefor paragraph in text_frame.paragraphs:for run in paragraph.runs:# 修改字体pptx_ea_font.set_font(run,new_font)#以下方法只能修改数字和英文#run.font.name new_fontif new_size :run.font.size Pt(new_size)if bold is not None:run.font.bold bold# 保存修改后的PPT文件new_ppt_file ppt_file.replace(.pptx, _new.pptx)presentation.save(new_ppt_file)print(字体修改完毕)以上代码只能修改文本框如果要修改图形中的字体需要用VBA。altF11 插入模块复制以下代码 按F5 代码来自 TomasZh 注意以下代码依然不能修改 图表 chart中的文本
Sub SetAllFontToYahei()set all fonts to 微软雅黑Dim sld As SlideDim shp As Shape, chd As ShapeDim i, jFor Each sld In ActivePresentation.Slidesi i 1Debug.Print Slide iFor Each shp In sld.Shapesj j 1Debug.Print vbTab Shape jIf shp.Type msoGroup ThenFor Each chd In shp.GroupItemsIf chd.HasTextFrame Thenchd.TextFrame.TextRange.Font.Name 微软雅黑chd.TextFrame.TextRange.Font.NameFarEast 微软雅黑End IfNextElseIf shp.HasTextFrame Thenshp.TextFrame.TextRange.Font.Name 微软雅黑shp.TextFrame.TextRange.Font.NameFarEast 微软雅黑End IfNextNextMsgBox Task completed!End Sub二、将文本框中的字都放到word里
def extract_text_from_ppt(ppt_file, word_file):# 打开PPT文件presentation Presentation(ppt_file)# 创建新的Word文档word_doc Document()# 循环遍历每个slidefor slide in presentation.slides:# 循环遍历slide中的每个shapefor shape in slide.shapes:# 检查shape类型是否为文本框if shape.has_text_frame:# 获取文本框中的文字text_frame shape.text_framefor paragraph in text_frame.paragraphs:# 提取文本到Word中word_doc.add_paragraph(paragraph.text)# 保存Word文档word_doc.save(word_file)print(文本提取完毕)
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/90078.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!