网站开发建设方案,网站提交入口百度,网站空间购买哪个好,网页前端开发框架目录 一、修改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(字体修改完毕)以上代码只能修改文本框因为图形是个msogroup对象。如果要修改图形中的字体需要用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以下代码更全面可以修改表格和图形的但是不能修改图表的。
Sub ChangeFontInAllSlides()Dim oSlide As SlideDim oShape As ShapeDim oTable As TableDim oRow As RowDim oCell As CellDim oTxtRange As TextRangeOn Error Resume NextFor Each oSlide In ActivePresentation.SlidesFor Each oShape In oSlide.ShapesIf oShape.HasTextFrame Then 处理文本框中的文本Set oTxtRange oShape.TextFrame.TextRangeWith oTxtRange.Font──────────────────────────────.Name Arial 修改为您所需的字体名称.Size 20 修改为您所需的字体大小.Color.RGB RGB(255, 0, 0) 修改为您所需的字体颜色.Bold True 修改为您所需的是否加粗.Italic False 修改为您所需的是否倾斜.Underline False 修改为您所需的是否有下划线──────────────────────────────End WithEnd IfIf oShape.HasTable Then 处理表格中的文本Set oTable oShape.TableFor Each oRow In oTable.RowsFor Each oCell In oRow.CellsIf oCell.Shape.HasTextFrame ThenSet oTxtRange oCell.Shape.TextFrame.TextRangeWith oTxtRange.Font──────────────────────────────.Name Arial 修改为您所需的字体名称.Size 20 修改为您所需的字体大小.Color.RGB RGB(255, 0, 0) 修改为您所需的字体颜色.Bold True 修改为您所需的是否加粗.Italic False 修改为您所需的是否倾斜.Underline False 修改为您所需的是否有下划线──────────────────────────────End WithEnd IfNext oCellNext oRowEnd IfNext oShapeNext oSlideEnd 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(文本提取完毕)