Skip to content

提供一个简单脚本, 合并 PDF #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
OrezzerO opened this issue May 30, 2024 · 3 comments
Closed

提供一个简单脚本, 合并 PDF #173

OrezzerO opened this issue May 30, 2024 · 3 comments

Comments

@OrezzerO
Copy link

我使用 MarginNote 来阅读本书, 但是每本书都有很多PDF, 一个一个导入比较凌乱, 不好整理, 所以写了一个简单的 Python 脚本, 将 PDF 合并成一个.

import PyPDF2
import os
import glob


def merge_pdfs(pdf_list, output_path):
    pdf_merger = PyPDF2.PdfMerger()
    
    for pdf in pdf_list:
        pdf_merger.append(pdf)
    
    with open(output_path, 'wb') as output_file:
        pdf_merger.write(output_file)
        
def list_sorted_pdfs(directory):
    # 获取当前目录下的所有 PDF 文件
    pdf_files = glob.glob(os.path.join(directory, "*.pdf"))
    
    # 按字母顺序排序
    pdf_files.sort()
    
    return pdf_files     


merge_pdfs(list_sorted_pdfs("./"),"Book1.pdf")
@Visualize-ML
Copy link
Owner

谢谢你!

@zptsqh0202
Copy link

Image
合并之后显示这种,如何解决

@OrezzerO
Copy link
Author

OrezzerO commented Feb 5, 2025

Image 合并之后显示这种,如何解决

可以看下合并脚本的输出有没有什么异常吗,合并后的文件大小是否在正常范围内

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants