报错如下:

Traceback (most recent call last):
File "./pdf_merge.py", line 68, in <module>
main(sys.argv[1], sys.argv[2])
File "./pdf_merge.py", line 51, in main
blankpage.mergeTranslatedPage(pageobj, 0, 0)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2377, in mergeTranslatedPage
tx, ty], expand)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2328, in mergeTransformedPage
PageObject._addTransformationMatrix(page2Content, page2.pdf, ctm), ctm, expand)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2284, in _mergePage
page2Content, rename, self.pdf)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2189, in _contentStreamRename
op = operands[i]
KeyError: 0

解决:

  1. 打开 /Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py
  2. 在文件开头加上 import types
  3. 找到函数 _contentStreamRename

    修改函数:
    def _contentStreamRename(stream, rename, pdf):
if not rename:
return stream
stream = ContentStream(stream, pdf)
for operands, operator in stream.operations:
# for i in range(len(operands)):
# op = operands[i]
# if isinstance(op, NameObject):
# operands[i] = rename.get(op,op)
if type(operands) == types.ListType:
for i in range(len(operands)):
op = operands[i]
if isinstance(op, NameObject):
operands[i] = rename.get(op,op)
elif type(operands) == types.DictType:
for i in operands:
op = operands[i]
if isinstance(op, NameObject):
operands[i] = rename.get(op,op)
else:
raise KeyError("type of operands is %s" % type(operands))
return stream
_contentStreamRename = staticmethod(_contentStreamRename)

参考链接:https://github.com/mstamy2/PyPDF2/issues/196

PyPDF2.py 合并pdf时报错问题的更多相关文章

  1. word 转 PDF时报错

    利用微软自带的com组件,把word转化成Pdf,利用vs2012调试时没有问题,但是发布到IIS时出错,错误为: 检索 COM 类工厂中 CLSID 为 {} 的组件时失败,原因是出现以下错误: 8 ...

  2. django 运行python manage.py sqlall books 时报错 app has migration

    出现这个问题的原因是版本之前的不兼容,我用的django版本是1.8.6 而 这条python manage.py sqlall books 是基于django1.0版本的. 在django1.8.6 ...

  3. py+selenium运行时报错Can not connect to the Service IEDriverServer.exe

    问题: 运行用例时,出现报错(host文件已加入127.0.0.1 localhost): raise WebDriverException("Can not connect to the ...

  4. 使用PyPdf2合并PDF文件(没有空白、报错)

    使用PyPdf2合并PDF文件(没有空白.报错) 对于合并之后pdf空白,或者出现 'latin-1' codec can't encode characters in position 8-11: ...

  5. pypdf2:下载Americanlife网页生成pdf合并pdf并添加书签

    初步熟悉 安装 pip install pypdf2 合并并添加书签 #!/usr/bin/env python3.5 # -*- coding: utf-8 -*- # @Time : 2019/1 ...

  6. python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案

    python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案 importerror:no mo ...

  7. 配置python+mod_wsgi+apache 时 在浏览器中访问服务器时报错:Invalid HTTP_HOST header: 'XXXXX'. You may need to add u'XXXXX' to ALLOWED_HOSTS,在setting.py中添加‘*”无效的原因

    配置python+mod_wsgi+apache 时 在浏览器中访问服务器时报错:Invalid HTTP_HOST header: 'XXXXX'. You may need to add u'XX ...

  8. 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

    在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: ...

  9. 使用Python批量合并PDF文件(带书签功能)

    网上找了几个合并pdf的软件,发现不是很好用,一般都没有添加书签的功能. 又去找了下python合并pdf的脚本,发现也没有添加书签的功能的. 于是自己动手编写了一个小工具,使用了PyPDF2. 下面 ...

随机推荐

  1. Floyd弗洛伊德算法

    先看懂如何使用 用Java实现一个地铁票价计算程序 String station = "A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A1 ...

  2. Java 正则表达式 简单用法

    正则表达式的具体写法网上有很多了,这里只记录在 Java 中怎么使用. java.util.regex.Matcher.java.util.regex.Pattern 主要有: String.matc ...

  3. 🔥 LeetCode 热题 HOT 100(1-10)

    1. 两数之和 思路一:暴力遍历所有组合 class Solution { public int[] twoSum(int[] nums, int target) { for (int i = 0; ...

  4. 媒体应用视频超分AI神器!360P视频一键转换HD

    作为多媒体应用的开发者,你是否想为媒体播放器快速开发创新AI功能?例如: 在播放低画质视频过程中对其进行逐帧超分 让满屏飘飞的弹幕自动绕过画面的主体人物 HMS Core 6.0.0开放的多媒体管线服 ...

  5. DC-4 靶机渗透测试

    DC-4 渗透测试 冲冲冲,努力学习 .掌握 hydra ,nc反弹shell 记住你要干嘛, 找地方上传shell(大多以后台登录为切入点,再反弹shell),shell提权到root 操作机:ka ...

  6. git从远程仓库里拉取一条本地不存在的分支

    查看远程分支和本地分支 git branch -va 当我想从远程仓库里拉取一条本地不存在的分支时: git checkout -b 本地分支名 origin/远程分支名 例如: 切换远程分支 git ...

  7. Java面向对象09——super

    Super  main ​ package oop.demon01.demon05; ​ public class Application {     public static void main( ...

  8. Windows协议 Kerberos篇

    认证流程 角色 功能 Domain Controller 也就是域控 Key Distribution Center 秘钥分发中心,简称KDC,默认安装在域控里,包括AS.AD和TGS. Accoun ...

  9. 字节跳动Android面试凉凉,挥泪整理面筋,你不看看吗?

    想在金九银十找工作的现在可以开始准备了,这边给大家分享一下面试会遇到的问题. 找工作还是需要大家不要担心,由于我们干这一行的接触人本来就不多,难免看到面试官会紧张,主要是因为怕面试官问的答不上来,答不 ...

  10. 字节跳动Android实习面试难吗,应该如何应对?

    字节跳动的面试难不难其实很难去非常准确的定义,但是能肯定的一点是,字节跳动的面试题都很有水平,真正考察了该岗位在以后工作中需要的能力. 大学学的Java后面又自学Android方向,跟着老师在实验室做 ...