How do I change the hyperlinks in pdf using python? I am currently using a pyPDF2 to open up and loop through the pages. How do I actually scan for hyperlinks and then proceed to change the hyperlinks?

解决方案

So I couldn't get what you want using the pyPDF2 library.

I did however get something working with another library: pdfrw. This installed fine for me using pip in Python 3.6:

pip install pdfrw

Note: for the following I have been using this example pdf I found online which contains multiple links. Your mileage may vary with this.

import pdfrw

pdf = pdfrw.PdfReader("pdf.pdf") #Load the pdf

new_pdf = pdfrw.PdfWriter() #Create an empty pdf

for page in pdf.pages: #Go through the pages

for annot in page.Annots or []: #Links are in Annots, but some pages

#don't have links so Annots returns None

old_url = annot.A.URI

#>Here you put logic for replacing the URLs<

#Use the PdfString object to do the encoding for us.

# Note the brackets around the URL here.

new_url = pdfrw.objects.pdfstring.PdfString("(http://www.google.com)")

#Override the URL with ours.

annot.A.URI = new_url

new_pdf.addpage(page)

new_pdf.write("new.pdf")

python 修改pdf内容,如何使用python更改pdf内的超链接?相关推荐

  1. 用python修改文件内容修改txt内容的3种方法

    用python修改文件内容修改txt内容的3种方法 方法一.修改原文件方式 def updateFile(file,old_str,new_str):"""替换文件中的字 ...

  2. python的基础_python基础知识,python必背内容,一、python的基

    python基础知识,python必背内容,一.python的基 一.python的基础 1.简单写一行python代码:print('hello,world')print('你好,北京') 2.变量 ...

  3. python基础知识-python基础知识,python必背内容,一、python的基

    python基础知识,python必背内容,一.python的基 一.python的基础 1.简单写一行python代码:print("hello,world')print("你好 ...

  4. python修改文件内容_python批量修改文件夹及其子文件夹下的文件内容

    前言:前几天我看一位同学要修改很多文件中的数据,该文件数据很规律,一行只有三个数,需要将每行最后一个数字改为负数,但文件有上千个,分布在每个文件夹下面以及它的多级子文件夹下,看他用excel手动改数据 ...

  5. python修改文件内容_Python批量修改文本文件内容的方法详解

    这篇文章主要介绍了Python批量修改文本文件内容的方法的相关资料,需要的朋友可以参考下 Python批量替换文件内容,支持嵌套文件夹 import os path="./" fo ...

  6. python修改文件内容,不需要read,write多个动作。

    python  要修改文件内容,常用 是先read,后write , 再 rename,很不爽. 比如:需要 把       yuv_dir ="../HD/"   # " ...

  7. Python 修改文件内容3种方法(替换文件内容)

    一.修改原文件方式 def alter(file,old_str,new_str):"""替换文件中的字符串:param file:文件名:param old_str:就 ...

  8. python修改html内容_详解Python利用Beautiful Soup模块修改内容示例代码

    Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python 库.它能够通过你喜欢的转换器实现惯用的文档导航.查找.修改文档的方式.他还能够修改HTML/XML文档的内容.这篇 ...

  9. python修改散点图中点的颜色_更改散点图中不同虚拟值的点的颜色 - python

    在我的数据集中,我有一个Price列用于房价,还有5个虚拟列用于城市中的不同位置.我要做的是用不同的颜色在散点图上显示数据点. 例如,在一个包含所有房屋价格的散点图上,我想要: 当dummy1表示房屋 ...

  10. 通过python修改cmd路径_cmd中如何更改文件路径 - 卡饭网

    怎么在PS中设置更改文件默认保存位置 怎么在PS中设置更改文件默认保存位置 接下介绍怎么在PS中设置更改文件默认保存位置具体如下:1. 第一步,进入PS,选择页面上方栏目中"编辑" ...

最新文章

  1. zabbix 3.2.1 升级3.4.1
  2. 【新星计划】MATLAB系统辨识工具箱使用方法
  3. adult道具项目开发 - 模式
  4. 怎么停止skywalking_Skywalking部署常见问题以及注意事项
  5. linux内核mtd分区,linux-kernel – 在运行时调整MTD分区大小
  6. 大厂后端必备分布式,一定要好好学|文末有1元福利
  7. mysql 连边聚合_MySQL分组,聚合函数,连表查询,子查询
  8. 如何挖掘大数据的价值
  9. 微博表情 文本与图片对照表
  10. (3/3) plsql使用技巧PL/SQL Developer实现双击table表名查询
  11. 【FFmpeg4.1.4】音视频分离器
  12. xbee模块和单片机_XBee® ZigBee 模块使用方法
  13. IC学习笔记9——多比特信号的跨时钟域处理方法之“MUX/DMUX同步器”
  14. 我的世界神秘时代研究笔记计算机,我的世界神秘时代5教程全研究笔记连线一览...
  15. python练习题5:货币转换 I
  16. Apple Pay 究竟是什么
  17. opencv实现matlab的median()函数
  18. 使用instantclient_19客户端(免安装)远程连接Oracle服务器端数据库
  19. 快速搭建springboot+mybatis-plus代码自动生成器的后端框架
  20. 疯狂java学习笔记三

热门文章

  1. 2017年3月份前端资源分享
  2. Golang 笔记 2 函数、结构体、接口、指针
  3. 用友u8数据库表结构
  4. 温故js系列(11)-BOM
  5. 一些Layout的坑
  6. ElasticSearch 2 (16) - 深入搜索系列之近似度匹配
  7. Devexpress 15.1.8 Breaking Changes
  8. Android四大组件之——Activity(一)定义、状态和后退栈(图文详解)
  9. Node Introduce
  10. 微软1月补丁星期二值得关注的蠕虫及其它