如何用python插入獨創(chuàng)性聲明
想必寫畢設的時候,大家都會遇到一個問題,那就是得在明評版的論文里面插入一個獨創(chuàng)性聲明。就因為這個事情,我折騰了好久,各種在線網站都試過了,然而基本都需要充值或者會員啥的。(小聲嚷嚷:“萬惡的資本”)
害~一不做二不休,我干脆自己寫個小工具好了。
一、代碼分析
利用PyPDF2庫便可輕松地對PDF文件進行處理,具體用法大家可以參考這里。首先是安裝這個庫:
pip install PyPDF2
定義輸入和輸出對象:
# 定義輸出對象
outputName = 'output.pdf'
output = PdfFileWriter()
# 定義讀取對象
thesisPDF = PdfFileReader(open(thesisName,'rb'))
insertPDF = PdfFileReader(open(insertName,'rb'))
N_page = thesisPDF.getNumPages()
pos = int(input('論文一共有"%d"頁,請輸入需要插入的位置:'%N_page))
分別讀取論文的PDF和獨創(chuàng)性聲明的PDF,隨后將聲明插入到論文中的指定頁面:
# 將聲明插入到指定頁面 for i in range(pos): output.addPage(thesisPDF.getPage(i)) output.addPage(insertPDF.getPage(0)) # 插入 for i in range(pos,N_page): output.addPage(thesisPDF.getPage(i))
將結果保存到本地:
# 保存插入后的結果 output.write(open(outputName,'wb'))
到這里,我們就已經成功的把聲明插入到指定的頁面中了。你沒有看錯,就是這么簡單~
二、完整代碼
將以上幾部分整合起來,完整的代碼如下:
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 5 20:13:18 2020
@author: kimol_love
"""
import os
from PyPDF2 import PdfFileWriter, PdfFileReader
# 用戶輸入論文名
while True:
thesisName = input('請輸入論文的文件名:')
if not os.path.exists(thesisName):
print('文件不存在,請重新輸入!')
continue
if thesisName[-4:].lower() != '.pdf':
print('后綴錯誤,請重新輸入!')
continue
break
# 用戶輸入需要插入的頁面
while True:
insertName = input('請輸入聲明的文件名:')
if not os.path.exists(insertName):
print('文件不存在,請重新輸入!')
continue
if thesisName[-4:].lower() != '.pdf':
print('后綴錯誤,請重新輸入!')
continue
break
# 定義輸出對象
outputName = 'output.pdf'
output = PdfFileWriter()
# 定義讀取對象
thesisPDF = PdfFileReader(open(thesisName,'rb'))
insertPDF = PdfFileReader(open(insertName,'rb'))
N_page = thesisPDF.getNumPages()
pos = int(input('論文一共有"%d"頁,請輸入需要插入的位置:'%N_page))
# 將聲明插入到指定頁面
for i in range(pos):
output.addPage(thesisPDF.getPage(i))
output.addPage(insertPDF.getPage(0)) # 插入
for i in range(pos,N_page):
output.addPage(thesisPDF.getPage(i))
# 保存插入后的結果
output.write(open(outputName,'wb'))
print('"%s"已經成功插入到"%s"的第%d頁'%(insertName,thesisName,pos))
運行效果如下:

打開生成的output.pdf,可以發(fā)現(xiàn)已經成功插入。
寫在最后
最后,感謝各位大大的耐心閱讀,咋們下次再會~
以上就是如何用python插入獨創(chuàng)性聲明的詳細內容,更多關于用python插入獨創(chuàng)性聲明的資料請關注腳本之家其它相關文章!
相關文章
安裝python依賴包psycopg2來調用postgresql的操作
這篇文章主要介紹了安裝python依賴包psycopg2來調用postgresql的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Python RuntimeWarning:invalid value encounter
這篇文章主要介紹了Python RuntimeWarning:invalid value encountered in double_scalars處理,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

