在工作中经常收到许多电子发票,需要打印出来报销,每张发票都要单独占用一张A4纸,大部分的空白部分财务还会要求裁掉。很是浪费纸张。
为了解决这个问题,我写了一个小工具,可以把两张、三张发票合并到一张A4纸。
下载地址:https://download.csdn.net/download/weixin_44554695/83366211
使用Python PyPDF2和tkinter,主要的代码如下:
## 二合一模式下选择两个文件合并:
def merge_2to1_2(self,file1,file2):
writer = PdfFileWriter()
pdf1 = PdfFileReader(file1)
page1 = pdf1.getPage(0)
h1 = float(page1.mediaBox.getHeight())
if h1>397:
filename = self.split_pdf(file1)
pdf1 = PdfFileReader(filename)
page1 = pdf1.getPage(0)
pdf2 = PdfFileReader(file2)
page2 = pdf2.getPage(0)
h2 = float(page2.mediaBox.getHeight())
if h2>397:
filename = self.split_pdf(file2)
pdf2 = PdfFileReader(filename)
page2 = pdf1.getPage(0)
blankpage = pdf.PageObject.createBlankPage(None,page1.mediaBox.getWidth(),page1.mediaBox.getHeight()+page2.mediaBox.getHeight())
blankpage.mergeScaledTranslatedPage(page1,0.9,10,page2.mediaBox.getHeight())
blankpage.mergeScaledTranslatedPage(page2,0.9,10,0)
writer.addPage(blankpage)
writer.removeLinks()
filename = os.path.basename(file1).split('.')[0]+"、"+os.path.basename(file2).split('.')[0]+"等发票合并.pdf"
self.res_file_name=filename
writer.write(open(filename,'wb'))
self.message()
self.reset_entry_text()
## 三合一模式下选择三个文件合并:
def merge_3to1_3(self,file1,file2,file3):
writer = PdfFileWriter()
pdf1 = PdfFileReader(file1)
page1 = pdf1.getPage(0)
h1 = float(page1.mediaBox.getHeight())
w1 = float(page1.mediaBox.getWidth())
if h1>397:
filename = self.split_pdf(file1)
pdf1 = PdfFileReader(filename)
page1 = pdf1.getPage(0)
h1 = float(page1.mediaBox.getHeight())
w1 = float(page1.mediaBox.getWidth())
pdf2 = PdfFileReader(file2)
page2 = pdf2.getPage(0)
h2 = float(page2.mediaBox.getHeight())
if h2>397:
filename = self.split_pdf(file2)
pdf2 = PdfFileReader(filename)
page2 = pdf1.getPage(0)
h2 = float(page2.mediaBox.getHeight())
pdf3 = PdfFileReader(file3)
page3 = pdf3.getPage(0)
h3 = float(page3.mediaBox.getHeight())
if h3>397:
filename = self.split_pdf(file3)
pdf3 = PdfFileReader(filename)
page3 = pdf1.getPage(0)
h3 = float(page3.mediaBox.getHeight())
blankpage = pdf.PageObject.createBlankPage(None,w1,(h1+h2+h3)/3*2)
blankpage.mergeScaledTranslatedPage(page1,2/3,100,(h2+h3)/3*2)
blankpage.mergeScaledTranslatedPage(page2,2/3,100,h3/3*2)
blankpage.mergeScaledTranslatedPage(page3,2/3,100,0)
writer.addPage(blankpage)
writer.removeLinks()
filename = os.path.basename(file1).split('.')[0]+"、"+os.path.basename(file2).split('.')[0]+"、"+os.path.basename(file3).split('.')[0]+"等发票合并.pdf"
self.res_file_name=filename
writer.write(open(filename,'wb'))
self.message()
self.reset_entry_text()
合并后效果如下:
1.二合一:
2.三合一: