浏览,打开PDF文件,在第1页添加戳记,然后将其另存为新文件

2024-09-29 23:17:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这些代码行来浏览/打开pdf,在第1页添加邮票/图像,然后将其另存为新文件。但是当我运行代码时,它并没有保存新的pdf文件。非常感谢您的帮助,我对python非常陌生

import tkinter
import fitz
from tkinter import messagebox
from tkinter import filedialog


main_win = tkinter.Tk()
main_win.geometry("500x500")
main_win.sourceFolder = ''
main_win.sourceFile = ''
def chooseDir():
    main_win.sourceFolder =  filedialog.askdirectory(parent=main_win, initialdir= "/", title='Please select a directory')

b_chooseDir = tkinter.Button(main_win, text = "Chose Folder", width = 20, height = 3, command = chooseDir)
b_chooseDir.place(x = 50,y = 50)
b_chooseDir.width = 100


def chooseFile():
    main_win.sourceFile = filedialog.askopenfilename(parent=main_win, initialdir= "/", title='Please select a directory')

def convertFile():
    dst_pdf_filename = 'destination.pdf'
    img_filename = 'hillsborough county stamp.png'
  
    img_rect = fitz.Rect(55, 28, 180, 390)
    
    page = document[0]
    page.insertImage(img_rect, filename=img_filename)
  
    document.save(dst_pdf_filename)
    document.close()
    
b_chooseFile = tkinter.Button(main_win, text = "Chose File", width = 20, height = 3, command = chooseFile)
b_chooseFile.place(x = 250,y = 50)
b_chooseFile.width = 100

b_convertFile = tkinter.Button(main_win, text = "Convert File", width = 20, height = 3, command = convertFile)
b_convertFile.place(x = 250,y = 200)
b_convertfile.width = 100


main_win.mainloop()
print(main_win.sourceFolder)
print(main_win.sourceFile )


Tags: importimgpdfmaintkinterdeffilenamewidth
1条回答
网友
1楼 · 发布于 2024-09-29 23:17:56

据我所知,您希望在源文件main_win.sourceFile中添加戳记图像并将其保存到新文件dst_pdf_filename

def convertFile():
    if main_win.sourceFile:
        dst_pdf_filename = 'destination.pdf'
        img_filename = 'hillsborough county stamp.png'

        img_rect = fitz.Rect(55, 28, 180, 390)

        document = fitz.open(main_win.sourceFile) # open source file
        page = document[0]
        page.insertImage(img_rect, filename=img_filename)

        document.save(dst_pdf_filename)
        document.close()

相关问题 更多 >

    热门问题