python递归统计文件夹下pdf文件的数量

import os
from docx import Document


def count_all_pages(root_dir):
    total_pages = 0
    # 遍历文件夹
    for dirpath, dirnames, filenames in os.walk(root_dir):
        for filename in filenames:
            # if filename.endswith('.docx') or filename.endswith('.doc'):
            #     total_pages+=1
            if filename.endswith('.pdf'):
                total_pages+=1
    return total_pages

# 要遍历的文件夹路径
folder_path = r'path'

# 调用函数计算页数总和
total_pages = count_all_pages(folder_path)
print(f'所有文档数为: {total_pages}')