如何用python插入獨(dú)創(chuàng)性聲明
想必寫(xiě)畢設(shè)的時(shí)候,大家都會(huì)遇到一個(gè)問(wèn)題,那就是得在明評(píng)版的論文里面插入一個(gè)獨(dú)創(chuàng)性聲明。就因?yàn)檫@個(gè)事情,我折騰了好久,各種在線網(wǎng)站都試過(guò)了,然而基本都需要充值或者會(huì)員啥的。(小聲嚷嚷:“萬(wàn)惡的資本”)害~一不做二不休,我干脆自己寫(xiě)個(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)行效果如下:

打開(kāi)生成的output.pdf,可以發(fā)現(xiàn)已經(jīng)成功插入。
寫(xiě)在最后最后,感謝各位大大的耐心閱讀,咋們下次再會(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. PHP laravel實(shí)現(xiàn)導(dǎo)出PDF功能2. Python-openpyxl表格讀取寫(xiě)入的案例詳解3. JavaScript實(shí)現(xiàn)留言板實(shí)戰(zhàn)案例4. 資深程序員:給Python軟件開(kāi)發(fā)測(cè)試的25個(gè)忠告!5. Python使用Selenium自動(dòng)進(jìn)行百度搜索的實(shí)現(xiàn)6. JS中6個(gè)對(duì)象數(shù)組去重的方法7. 使用Blazor框架實(shí)現(xiàn)在前端瀏覽器中導(dǎo)入和導(dǎo)出Excel8. ASP基礎(chǔ)知識(shí)Command對(duì)象講解9. vscode運(yùn)行php報(bào)錯(cuò)php?not?found解決辦法10. python中文本字符處理的簡(jiǎn)單方法記錄

網(wǎng)公網(wǎng)安備