본문 바로가기
IT, 프로그램, SW개발/Python,파이썬, AI

[Python/파이썬] drag&drop으로 ppt(파워포인트)파일 폰트 자동 변경하기

by RedBaDa 2023. 3. 6.

우선 코드부터 설명은 나중에 기록....

 

win32com, pathlib, pyQt5 필요

 

 

import win32com.client
from pathlib import Path
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtGui import QIcon
import os



def ChangeFont(filepath, FName):
    powerpoint = win32com.client.Dispatch('PowerPoint.Application')
    Sepa = '\\'
   
    print(filepath)
   
    fextention = os.path.splitext(filepath)
    print(fextention)

    dstPath = fextention[0] + '_changed' + fextention[1]
    print(dstPath)

    filepath = filepath.replace("/","\\")
    dstPath = dstPath.replace("/","\\")
    print(filepath)

    ppt = powerpoint.Presentations.Open (filepath, WithWindow=True)


    for slide in ppt.Slides:
        for shape in slide.shapes:
            if shape.HasTextFrame == -1:
                shape.TextFrame.TextRange.Font.NameFarEast = FName
                shape.TextFrame.TextRange.Font.Name = FName
            if shape.HasTable == -1:
                for row in shape.Table.Rows:
                    for cell in row.cells:
                        cell.Shape.TextFrame.TextRange.Font.NameFarEast = FName
                        cell.Shape.TextFrame.TextRange.Font.Name = FName
            try:
                for GI in shape.GroupItems:
                    GI.TextFrame.TextRange.Font.NameFarEast = FName
                    GI.TextFrame.TextRange.Font.Name = FName
            except:
                pass

    ppt.SaveAs (dstPath)
    ppt.Close ()


Fname = '맑은고딕'


class MainWidget(QtWidgets.QMainWindow):
   

    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)
        self.setWindowTitle("ppt font converter")
        self.resize(600,400)
        self.setAcceptDrops(True)
        self.setWindowIcon(QIcon("./filled_icon.ico"))
        self.lb_query = QtWidgets.QLabel("[입력 문자열]",self)
        self.Initialize()
 
    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.accept()
        else:
            event.ignore()
 
    def dropEvent(self, event):
        files = [u.toLocalFile() for u in event.mimeData().urls()]
        fname = ''
        for f in files:
            print(f)
            fname += f
        print(fname)
        self.lb_query.setText(fname+' 을 변환 완료했습니다!!')
        ChangeFont(fname,Fname)


    def Initialize(self):

        self.lb_query.setGeometry(QtCore.QRect(80, 200, 800, 40)) #x, y, w, h
        self.lb_query.setFont(QtGui.QFont("맑은고딕",12))        
        self.lb_query.setText('여기에 폰트를 변경할 ppt파일을 drag & drop 해주세요!!')
   
    def showText(self,fname):
        self.lb_query.setText(fname)

 
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = MainWidget()
    MainWindow.show()
    sys.exit(app.exec_())


반응형