如何用python插入独创性声明
作者:kimol君 时间:2021-05-07 04:24:15
目录
一、代码分析
二、完整代码
写在最后
想必写毕设的时候,大家都会遇到一个问题,那就是得在明评版的论文里面插入一个独创性声明。就因为这个事情,我折腾了好久,各种在线网站都试过了,然而基本都需要充值或者会员啥的。(小声嚷嚷:“万恶的资本”)
害~一不做二不休, * 脆自己写个小工具好了。
一、代码分析
利用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和独创性声明的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,可以发现已经成功插入。
写在最后
最后,感谢各位大大的耐心阅读,咋们下次再会~
来源:https://blog.csdn.net/kimol_justdo/article/details/109523768
标签:python,插入声明
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一个挺酷的星级投票效果
2010-08-03 12:44:00
![](https://img.aspxhome.com/file/UploadPic/20108/3/star-63s.png)
Python编解码问题及文本文件处理方法详解
2021-04-13 07:52:06
![](https://img.aspxhome.com/file/2023/9/72999_0s.png)
Python基于pandas实现json格式转换成dataframe的方法
2021-08-23 23:09:01
浅谈python opencv对图像颜色通道进行加减操作溢出
2021-12-26 11:45:23
![](https://img.aspxhome.com/file/2023/3/99473_0s.jpg)
设计手机端应用时的一些建议
2011-05-14 16:45:00
Python中bytes和str的区别与联系详解
2022-04-18 18:36:57
界面设计10条可用性方面的启发
2010-04-06 17:22:00
![](https://img.aspxhome.com/file/UploadPic/20104/6/heuristics-95s.gif)
如何更优雅地写python代码
2022-03-03 04:53:24
JavaScript Dom编程:介绍学习书籍
2008-02-20 08:32:00
php版银联支付接口开发简明教程
2023-11-24 00:27:41
Python实现微信自动好友验证,自动回复,发送群聊链接方法
2021-10-22 00:11:23
![](https://img.aspxhome.com/file/2023/7/63227_0s.jpg)
Javascript DOM 编程实例讲解--仿LightBox效果提示框
2008-05-01 13:25:00
![](https://img.aspxhome.com/file/UploadPic/up/2008050116095611.gif)
mysql慢查询的分析方法
2010-08-03 14:51:00
斜角滑动门导航条 DIV+CSS
2008-07-19 15:45:00
![](https://img.aspxhome.com/file/UploadPic/20087/19/200871916554645s.gif)
漂亮的Django Markdown富文本app插件的实现
2021-08-14 15:23:24
![](https://img.aspxhome.com/file/2023/3/80533_0s.jpg)
Python全局变量用法实例分析
2023-08-03 13:39:18
Access下如何使用通用对话框
2008-11-20 16:41:00
python中format()函数的简单使用教程
2021-07-10 15:34:01
![](https://img.aspxhome.com/file/2023/9/93569_0s.png)
谈谈网页一屏有多大?
2007-12-21 12:28:00
![](https://img.aspxhome.com/file/UploadPic/200712/21/2007122112356981s.jpg)
tensorflow使用指定gpu的方法
2022-10-23 16:00:31