python 修改pdf内容,如何使用python更改pdf内的超链接?
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内的超链接?相关推荐
- 用python修改文件内容修改txt内容的3种方法
用python修改文件内容修改txt内容的3种方法 方法一.修改原文件方式 def updateFile(file,old_str,new_str):"""替换文件中的字 ...
- python的基础_python基础知识,python必背内容,一、python的基
python基础知识,python必背内容,一.python的基 一.python的基础 1.简单写一行python代码:print('hello,world')print('你好,北京') 2.变量 ...
- python基础知识-python基础知识,python必背内容,一、python的基
python基础知识,python必背内容,一.python的基 一.python的基础 1.简单写一行python代码:print("hello,world')print("你好 ...
- python修改文件内容_python批量修改文件夹及其子文件夹下的文件内容
前言:前几天我看一位同学要修改很多文件中的数据,该文件数据很规律,一行只有三个数,需要将每行最后一个数字改为负数,但文件有上千个,分布在每个文件夹下面以及它的多级子文件夹下,看他用excel手动改数据 ...
- python修改文件内容_Python批量修改文本文件内容的方法详解
这篇文章主要介绍了Python批量修改文本文件内容的方法的相关资料,需要的朋友可以参考下 Python批量替换文件内容,支持嵌套文件夹 import os path="./" fo ...
- python修改文件内容,不需要read,write多个动作。
python 要修改文件内容,常用 是先read,后write , 再 rename,很不爽. 比如:需要 把 yuv_dir ="../HD/" # " ...
- Python 修改文件内容3种方法(替换文件内容)
一.修改原文件方式 def alter(file,old_str,new_str):"""替换文件中的字符串:param file:文件名:param old_str:就 ...
- python修改html内容_详解Python利用Beautiful Soup模块修改内容示例代码
Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python 库.它能够通过你喜欢的转换器实现惯用的文档导航.查找.修改文档的方式.他还能够修改HTML/XML文档的内容.这篇 ...
- python修改散点图中点的颜色_更改散点图中不同虚拟值的点的颜色 - python
在我的数据集中,我有一个Price列用于房价,还有5个虚拟列用于城市中的不同位置.我要做的是用不同的颜色在散点图上显示数据点. 例如,在一个包含所有房屋价格的散点图上,我想要: 当dummy1表示房屋 ...
- 通过python修改cmd路径_cmd中如何更改文件路径 - 卡饭网
怎么在PS中设置更改文件默认保存位置 怎么在PS中设置更改文件默认保存位置 接下介绍怎么在PS中设置更改文件默认保存位置具体如下:1. 第一步,进入PS,选择页面上方栏目中"编辑" ...
最新文章
- zabbix 3.2.1 升级3.4.1
- 【新星计划】MATLAB系统辨识工具箱使用方法
- adult道具项目开发 - 模式
- 怎么停止skywalking_Skywalking部署常见问题以及注意事项
- linux内核mtd分区,linux-kernel – 在运行时调整MTD分区大小
- 大厂后端必备分布式,一定要好好学|文末有1元福利
- mysql 连边聚合_MySQL分组,聚合函数,连表查询,子查询
- 如何挖掘大数据的价值
- 微博表情 文本与图片对照表
- (3/3) plsql使用技巧PL/SQL Developer实现双击table表名查询
- 【FFmpeg4.1.4】音视频分离器
- xbee模块和单片机_XBee® ZigBee 模块使用方法
- IC学习笔记9——多比特信号的跨时钟域处理方法之“MUX/DMUX同步器”
- 我的世界神秘时代研究笔记计算机,我的世界神秘时代5教程全研究笔记连线一览...
- python练习题5:货币转换 I
- Apple Pay 究竟是什么
- opencv实现matlab的median()函数
- 使用instantclient_19客户端(免安装)远程连接Oracle服务器端数据库
- 快速搭建springboot+mybatis-plus代码自动生成器的后端框架
- 疯狂java学习笔记三