python,操作pdf文件以及普通文件…

首先,需要安装插件: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()) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值