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)
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()
-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
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