首先,需要安装插件:pypdf
output = PdfFileWriter()
#读取pdf文件
input1 = PdfFileReader(file('/home/zilu/workspace/liuy/project/caifujutou/static/xml/test.pdf', 'rb'))
output.addPage(input1.getPage(0))
outputStrem = open('/home/zilu/workspace/liuy/project/caifujutou/static/xml/test.pdf', 'rw')
#写入结果pdf文件
output.write(outputStrem)
outputStrem.close()
#操作普通文件类型
#filename,文件路径、名称,如果有\,注意转义或者开头加r,mode读取的模式,r读取,w写入,,
f = open('/home/zilu/workspace/liuy/project/caifujutou/static/xml/test.xml', 'r')
#获取内容并储存在变量里
read = f.read()
print(read)
#关闭文件,节省内存
f.close()
#读取pdf文件写入新的pdf文件
by = open('/home/zilu/workspace/liuy/project/caifujutou/static/xml/test.pdf', 'rb')
by.seek(0, 0)
outputStrem = open('/home/zilu/workspace/liuy/project/caifujutou/static/xml/test2.pdf', 'w')
outputStrem.write(by.read())
output = PdfFileWriter()
#读取pdf文件
input1 = PdfFileReader(file('/home/zilu/workspace/liuy/project/caifujutou/static/xml/test.pdf', 'rb'))
output.addPage(input1.getPage(0))
outputStrem = open('/home/zilu/workspace/liuy/project/caifujutou/static/xml/test.pdf', 'rw')
#写入结果pdf文件
output.write(outputStrem)
outputStrem.close()
#操作普通文件类型
#读取pdf文件写入新的pdf文件