您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章给大家分享的是有关怎么使用python插入独创性声明的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
利用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,可以发现已经成功插入。
感谢各位的阅读!关于“怎么使用python插入独创性声明”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。