Show TOC entries
This commit is contained in:
parent
b774b90504
commit
0d9dbd7cb4
35
src/main.py
35
src/main.py
|
@ -7,29 +7,46 @@ from PIL import Image, ImageTk
|
||||||
|
|
||||||
CURRENT_PAGE = 0
|
CURRENT_PAGE = 0
|
||||||
|
|
||||||
def main():
|
|
||||||
|
def main() -> None:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("file")
|
parser.add_argument("file")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
doc: pymupdf.Document
|
doc: pymupdf.Document
|
||||||
with pymupdf.open(args.file) as doc:
|
with pymupdf.open(args.file) as doc:
|
||||||
|
toc = doc.get_toc()
|
||||||
|
|
||||||
|
def toc_entry_for_page(page_num: int) -> str:
|
||||||
|
last_title = ""
|
||||||
|
for lvl, t, p in toc:
|
||||||
|
if (p - 1) > page_num:
|
||||||
|
return last_title
|
||||||
|
if (p - 1) == page_num:
|
||||||
|
return t
|
||||||
|
last_title = t
|
||||||
|
|
||||||
root = tkinter.Tk()
|
root = tkinter.Tk()
|
||||||
frm = ttk.Frame(root, padding=10)
|
frm = ttk.Frame(root, padding=10)
|
||||||
frm.grid()
|
frm.grid()
|
||||||
|
|
||||||
label = ttk.Label(frm)
|
page_title = ttk.Label(frm)
|
||||||
label.grid(column=1, row=0)
|
page_title.grid(column=1, row=0)
|
||||||
|
|
||||||
def load_page(i: int):
|
page_view = ttk.Label(frm)
|
||||||
page = doc[i]
|
page_view.grid(column=1, row=1)
|
||||||
|
|
||||||
|
def load_page(page_num: int):
|
||||||
|
page = doc[page_num]
|
||||||
pix = page.get_pixmap()
|
pix = page.get_pixmap()
|
||||||
|
|
||||||
mode = "RGBA" if pix.alpha else "RGB"
|
mode = "RGBA" if pix.alpha else "RGB"
|
||||||
img = Image.frombytes(mode, [pix.width, pix.height], pix.samples)
|
img = Image.frombytes(mode, [pix.width, pix.height], pix.samples)
|
||||||
img = ImageTk.PhotoImage(img)
|
img = ImageTk.PhotoImage(img)
|
||||||
label.configure(image=img)
|
page_view.configure(image=img)
|
||||||
label.image = img
|
page_view.image = img
|
||||||
|
|
||||||
|
page_title.configure(text=toc_entry_for_page(page_num))
|
||||||
|
|
||||||
def next_page():
|
def next_page():
|
||||||
global CURRENT_PAGE
|
global CURRENT_PAGE
|
||||||
|
@ -41,8 +58,8 @@ def main():
|
||||||
CURRENT_PAGE -= 1
|
CURRENT_PAGE -= 1
|
||||||
load_page(CURRENT_PAGE)
|
load_page(CURRENT_PAGE)
|
||||||
|
|
||||||
ttk.Button(frm, text="Previous", command=previous_page).grid(column=0, row=0)
|
ttk.Button(frm, text="Previous", command=previous_page).grid(column=0, row=1)
|
||||||
ttk.Button(frm, text="Next", command=next_page).grid(column=2, row=0)
|
ttk.Button(frm, text="Next", command=next_page).grid(column=2, row=1)
|
||||||
|
|
||||||
load_page(CURRENT_PAGE)
|
load_page(CURRENT_PAGE)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue