#!/usr/bin/env python3
import PyPDF2
import sys

def pdf_to_txt(pdf_path, txt_path):
    try:
        with open(pdf_path, 'rb') as pdf_file:
            pdf_reader = PyPDF2.PdfReader(pdf_file)
            text_content = ""
            
            for page_num in range(len(pdf_reader.pages)):
                page = pdf_reader.pages[page_num]
                text_content += f"--- 第{page_num + 1}页 ---\n"
                text_content += page.extract_text()
                text_content += "\n\n"
            
            with open(txt_path, 'w', encoding='utf-8') as txt_file:
                txt_file.write(text_content)
            
            print(f"转换完成: {pdf_path} -> {txt_path}")
            print(f"总页数: {len(pdf_reader.pages)}")
            
    except Exception as e:
        print(f"转换失败: {e}")

if __name__ == "__main__":
    pdf_file = "人性解读之金瓶梅.pdf"
    txt_file = "人性解读之金瓶梅.txt"
    pdf_to_txt(pdf_file, txt_file)
