浏览,打开 PDF 文件,在第 1 页添加图章,然后另存为新文件 python
Browse, open a PDF file, add a stamp to page 1, then save it as a new file python
我将这些代码行 browse/open 成一个 pdf,将 stamp/image 添加到第 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()
我将这些代码行 browse/open 成一个 pdf,将 stamp/image 添加到第 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()