[Python] GUI for inserting TeX format into PowerPoint with 2 clicks [PowerPoint]

Introduction

Can you type in the button menu to create slide materials that make heavy use of mathematical formulas? is what I think. However, trying to use TeX with PowerPoint takes a lot of wasted effort and is troublesome.

Let's solve such a problem ** by making a very simple tool **. (Only compatible with Windows)

What was made

ldfsi-r7a10.gif

code

from urllib.parse import quote
from urllib.request import urlopen
import os
import tkinter
from tkinter import ttk
import pyperclip as ppc
import win32com.client
app = None
ppt = None
T = False


def setup():
    global app, ppt, T
    app = win32com.client.GetObject(Class="PowerPoint.Application")
    try:
        ppt = app.ActivePresentation
        if nop.get():
            app.CommandBars.ExecuteMso("InsertBuildingBlocksEquationsGallery")
            app.ActiveWindow.Selection.SlideRange.Shapes(
                app.ActiveWindow.Selection.SlideRange.Shapes.Count
            ).TextFrame.TextRange.Text = "Ⓣ"
            T = True
        else:
            T = False
        return True
    except:
        ppt = None
        return False


def isOpen():
    try:
        win32com.client.GetObject(Class="PowerPoint.Application")
        return True
    except:
        return False


def insertTeX():
    global index, texs
    math = tex.get("1.0", "end -1c")
    if not math or not isOpen():
        return
    if not T or not ppt or app != win32com.client.GetObject(
            Class="PowerPoint.Application"):
        if not setup(): return
    if nop.get():
        app.CommandBars.ExecuteMso("InsertBuildingBlocksEquationsGallery")
        app.ActiveWindow.Selection.SlideRange.Shapes(
            app.ActiveWindow.Selection.SlideRange.Shapes.Count
        ).TextFrame.TextRange.Text = " ".join(math.splitlines())
        app.CommandBars.ExecuteMso("EquationProfessional")
    else:
        with urlopen(
                f'https://texclip.marutank.net/render.php/texclip20201001121356.svg?s={quote(math)}&f=c&r=300&m=s&b=f&k=f'
        ) as svg:
            with open(
                    os.path.dirname(os.path.abspath(__file__)) + r"\tmp.svg",
                    "wb") as f:
                f.write(svg.read())
        app.ActiveWindow.Selection.SlideRange.Shapes.AddPicture(
            os.path.dirname(os.path.abspath(__file__)) + r"\tmp.svg", False,
            True, 0, 0)
        os.remove(os.path.dirname(os.path.abspath(__file__)) + r"\tmp.svg")
    if not texs or texs[-1] != math:
        texs.append(math)
        index += 1


def setTeX():
    tex.delete("1.0", "end -1c")
    tex.insert("1.0", ppc.paste())


def prev():
    global index
    if texs:
        index = max(index - 1, 0)
        tex.delete("1.0", "end -1c")
        tex.insert("1.0", texs[index])
    return


def next_():
    global index
    if texs:
        index = min(index + 1, len(texs) - 1)
        tex.delete("1.0", "end -1c")
        tex.insert("1.0", texs[index])
    return


main_win = tkinter.Tk()
main_win.title("TeX2PPT")
main_win.geometry("500x95")
main_win.minsize(main_win.winfo_width(), 95)

main_frm = ttk.Frame(main_win)
main_frm.grid(column=0, row=0, sticky=tkinter.NSEW, padx=5, pady=10)

index = -1
texs = []
tex = tkinter.Text(main_frm)
paste_btn = ttk.Button(main_frm, text="paste", command=setTeX)
insert_btn = ttk.Button(main_frm, text="Insert", command=insertTeX)
prev_btn = ttk.Button(main_frm, text="∧", command=prev, width=2)
next_btn = ttk.Button(main_frm, text="∨", command=next_, width=2)
nop = tkinter.IntVar()
nop.set(1)
rb1 = ttk.Radiobutton(main_frm, text='Formula', value=1, variable=nop)
rb2 = ttk.Radiobutton(main_frm, text='image', value=0, variable=nop)

tex.grid(column=0, row=0, sticky='nsew', rowspan=10, columnspan=10, padx=5)
paste_btn.grid(column=11, row=8, columnspan=2)
insert_btn.grid(column=11, row=9, columnspan=2)
prev_btn.grid(column=10, row=7)
next_btn.grid(column=10, row=9)
rb1.grid(column=11, row=7, sticky=tkinter.SE)
rb2.grid(column=12, row=7, sticky=tkinter.SE)

main_win.columnconfigure(0, weight=1)
main_win.rowconfigure(0, weight=1)
main_frm.columnconfigure(1, weight=1)
main_frm.rowconfigure(1, weight=1)
main_win.attributes("-topmost", True)
main_win.mainloop()

How to use

-Enter TeX format formula in the text box -Select whether to insert as a PowerPoint formula or to insert as an image ・ Click the insert button

Finally

Desmos When you copy a formula, it is in TeX format, so it is convenient to enter it in Desmos and copy → insert (rather it) Made for).

Recommended Posts

[Python] GUI for inserting TeX format into PowerPoint with 2 clicks [PowerPoint]
INSERT into MySQL with Python [For beginners]
Automatically format Python code into PEP8-compliant code with Emacs
Note for formatting numbers with python format function
Automatically paste images into PowerPoint materials with python + α
Summary of tools for operating Windows GUI with Python
[GUI with Python] PyQt5-Layout management-
Stylish technique for pasting CSV data into Excel with Python
Format json with Vim (with python)
[GUI with Python] PyQt5 -Preparation-
String format with Python% operator
[GUI with Python] PyQt5 -Paint-
[GUI with Python] PyQt5 -Widget II-
Let's make a GUI with python.
Getting Started with Python for PHPer-Classes
[GUI with Python] PyQt5-The first step-
[GUI with Python] PyQt5-Drag and drop-
[GUI with Python] PyQt5 -Custom Widget-
Getting Started with Python for PHPer-Functions
[Python] Format when to_csv with pandas
Edit Slide (PowerPoint for Google) with Python (Low-cost RPA case with Google API and Python)
Easy keyword extraction with TermExtract for Python
Creating a simple PowerPoint file with Python
WEB scraping with Python (for personal notes)
Manually ssh registration for coreserver with python
Use DeepL with python (for dissertation translation)
Memo to ask for KPI with python
Amplify images for machine learning with python
Tips for dealing with binaries in Python
Put protocol buffers into sqlite with python
Tips for using python + caffe with TSUBAME
[Shakyo] Encounter with Python for machine learning
Process multiple lists with for in Python
Getting Started with Python for PHPer-Super Basics
Debug for mysql connection with python mysql.connector
[Python] Read images with OpenCV (for beginners)
WebApi creation with Python (CRUD creation) For beginners
Happy GUI construction with electron and python
Preparation for scraping with python [Chocolate flavor]
[For beginners] Try web scraping with Python
Divide each PowerPoint slide into a JPG file and output it with python