如何用python插入獨(dú)創(chuàng)性聲明
想必寫畢設(shè)的時(shí)候,大家都會(huì)遇到一個(gè)問(wèn)題,那就是得在明評(píng)版的論文里面插入一個(gè)獨(dú)創(chuàng)性聲明。就因?yàn)檫@個(gè)事情,我折騰了好久,各種在線網(wǎng)站都試過(guò)了,然而基本都需要充值或者會(huì)員啥的。(小聲嚷嚷:“萬(wàn)惡的資本”)害~一不做二不休,我干脆自己寫個(gè)小工具好了。
一、代碼分析利用PyPDF2庫(kù)便可輕松地對(duì)PDF文件進(jìn)行處理,具體用法大家可以參考這里。首先是安裝這個(gè)庫(kù):
pip install PyPDF2
定義輸入和輸出對(duì)象:
# 定義輸出對(duì)象outputName = ’output.pdf’output = PdfFileWriter()# 定義讀取對(duì)象thesisPDF = PdfFileReader(open(thesisName,’rb’))insertPDF = PdfFileReader(open(insertName,’rb’))N_page = thesisPDF.getNumPages()pos = int(input(’論文一共有'%d'頁(yè),請(qǐng)輸入需要插入的位置:’%N_page))
分別讀取論文的PDF和獨(dú)創(chuàng)性聲明的PDF,隨后將聲明插入到論文中的指定頁(yè)面:
# 將聲明插入到指定頁(yè)面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))
將結(jié)果保存到本地:
# 保存插入后的結(jié)果output.write(open(outputName,’wb’))
到這里,我們就已經(jīng)成功的把聲明插入到指定的頁(yè)面中了。你沒(méi)有看錯(cuò),就是這么簡(jiǎn)單~
二、完整代碼將以上幾部分整合起來(lái),完整的代碼如下:
# -*- coding: utf-8 -*-'''Created on Thu Nov 5 20:13:18 2020@author: kimol_love'''import osfrom PyPDF2 import PdfFileWriter, PdfFileReader# 用戶輸入論文名while True: thesisName = input(’請(qǐng)輸入論文的文件名:’) if not os.path.exists(thesisName): print(’文件不存在,請(qǐng)重新輸入!’) continue if thesisName[-4:].lower() != ’.pdf’: print(’后綴錯(cuò)誤,請(qǐng)重新輸入!’) continue break# 用戶輸入需要插入的頁(yè)面while True: insertName = input(’請(qǐng)輸入聲明的文件名:’) if not os.path.exists(insertName): print(’文件不存在,請(qǐng)重新輸入!’) continue if thesisName[-4:].lower() != ’.pdf’: print(’后綴錯(cuò)誤,請(qǐng)重新輸入!’) continue break# 定義輸出對(duì)象outputName = ’output.pdf’output = PdfFileWriter()# 定義讀取對(duì)象thesisPDF = PdfFileReader(open(thesisName,’rb’))insertPDF = PdfFileReader(open(insertName,’rb’))N_page = thesisPDF.getNumPages()pos = int(input(’論文一共有'%d'頁(yè),請(qǐng)輸入需要插入的位置:’%N_page))# 將聲明插入到指定頁(yè)面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)) # 保存插入后的結(jié)果output.write(open(outputName,’wb’))print(’'%s'已經(jīng)成功插入到'%s'的第%d頁(yè)’%(insertName,thesisName,pos))
運(yùn)行效果如下:
打開生成的output.pdf,可以發(fā)現(xiàn)已經(jīng)成功插入。
寫在最后最后,感謝各位大大的耐心閱讀,咋們下次再會(huì)~
以上就是如何用python插入獨(dú)創(chuàng)性聲明的詳細(xì)內(nèi)容,更多關(guān)于用python插入獨(dú)創(chuàng)性聲明的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Docker 容器健康檢查機(jī)制2. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效3. ASP.NET MVC使用正則表達(dá)式驗(yàn)證手機(jī)號(hào)碼4. PHP接收json并將接收數(shù)據(jù)插入數(shù)據(jù)庫(kù)5. python datetime時(shí)間格式的相互轉(zhuǎn)換問(wèn)題6. 基于python實(shí)現(xiàn)數(shù)組格式參數(shù)加密計(jì)算7. Rollup 簡(jiǎn)易入門示例教程8. Python requests庫(kù)參數(shù)提交的注意事項(xiàng)總結(jié)9. php判斷一個(gè)請(qǐng)求是ajax請(qǐng)求還是普通請(qǐng)求的方法10. python 爬取京東指定商品評(píng)論并進(jìn)行情感分析
