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

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

对于合并之后pdf空白,或者出现 'latin-1' codec can't encode characters in position 8-11: ordinal not in range(256),请参照下面的链接对PyPdf2进行修改

https://blog.csdn.net/weixin_43116153/article/details/105218309

作者:小羊瓜瓜

修改完成后,便可以正常使用PyPdf2库对文件进行合并

代码如下:

import os
import PyPDF2

folder = r'D:\要合并的文档所在文件夹'
output = os.path.join(folder, 'output.pdf') # 输出文件
filelist = os.listdir(folder) # 获取pdf文件名
merger = PyPDF2.PdfFileMerger() 
for filename in filelist:
    path = os.path.join(folder, filename) # pdf的绝对路径
    print(path)
    merger.append(path, bookmark=filename[:-4]) # 合并并贴标签

with open(output, 'wb') as f:
    merger.write(f) # 输出

posted @ 2020-08-26 17:54  aminor  阅读(887)  评论(0)    收藏  举报
/**/ /**/