多层级文件夹的tif文件统计页数结果可以导出表格、可以选择文件夹,实现批量统计、提取最后一层文件夹名称,全部代码
时间: 2024-09-09 19:16:07 浏览: 53
处理多层级文件夹下的TIFF文件,并批量统计页数并导出为表格,你需要编写一个递归函数来遍历目录结构。这里提供一个基本的Python脚本示例,使用`os`和`Pillow`库:
```python
import os
import glob
from PIL import Image
import pandas as pd
def count_pages_in_dir(dir_path):
total_pages = []
for root, dirs, files in os.walk(dir_path):
for file in files:
if file.endswith(".tif"):
full_path = os.path.join(root, file)
try:
img = Image.open(full_path)
num_pages = len(img.getbands()) if img.mode == 'CMYK' else len(img.pages)
total_pages.append((root.split('/')[-1], num_pages)) # 提取最后一层文件夹名
except Exception as e:
print(f"无法打开文件 {full_path}: {e}")
return total_pages
def save_to_csv(total_pages, output_file):
df = pd.DataFrame(total_pages, columns=['Folder Name', 'Pages'])
df.to_csv(output_file, index=False)
# 选择文件夹路径
folder_path = input("请输入要统计的文件夹路径:")
results = count_pages_in_dir(folder_path)
save_to_csv(results, "result.csv")
print(f"统计完成,结果已保存至'result.csv'.")
阅读全文