[英]Browse, open a PDF file, add a stamp to page 1, then save it as a new file 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 )
據我了解,您想在源文件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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.