吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 562|回复: 28
收起左侧

[经验求助] 批处理脚本(.bat)或py可执行程序,获取文件夹中 Word 文档和pdf文档的路径、页数...

[复制链接]
SU150228 发表于 2023-8-28 00:09
40吾爱币
有没有这样一个批处理脚本(.bat)或py可执行程序:遍历文件夹,获取文件夹中 Word 文档和pdf文档的路径、页数和纸张大小三项功能,并将统计结果自动导出到paths.txt文件并存储到当前目录。
    注意:因为需要paths.txt导入到.xlsx中,所以路径、页数和纸张之间的统计结果须有分隔符(冒号不能用)
    本人是小白一枚,烦请大神出手帮助并调试好!!!
附:遍历文件夹,获取文件夹中 Word 文档和pdf文档的路径
@echo off  
setlocal enabledelayedexpansion  
  
REM 设置要遍历的文件夹路径  
set "folder=D:\PLDY"  
  
REM 遍历文件夹中的所有文件  
for /r "%folder%" %%F in (*.docx, *.doc, *.pdf) do (  
    REM 获取文件路径  
    set "filePath=%%~fF"  
  
    REM 输出文件路径到 txt 文件  
    echo !filePath! >> paths.txt  
)  

  
REM 完成操作,退出批处理脚本  
endlocal


大家好,在此收集批处理脚本bat或py可执行程序
有效期截止到2023年9月17日00:08,麻烦在此之前点击以下链接将文件发送给我,支持发送任意格式的文件,感谢~
链接:https://pan.baidu.com/disk/main#/transfer/send?surl=AB4AAAAAABE0ug

最佳答案

查看完整内容

发现有bug https://t.wss.ink/f/c223fj1ub4z 这个应该没问题

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

blfiag 发表于 2023-8-28 00:09
发现有bug
https://t.wss.ink/f/c223fj1ub4z  这个应该没问题
justfly99 发表于 2023-8-28 09:37
[Python] 纯文本查看 复制代码
import os
from pathlib import Path
import docx 
from PyPDF2 import PdfFileReader

results = []

for root, dirs, files in os.walk('path/to/folder'):
    for file in files:
        file_path = os.path.join(root, file)
        if file.endswith('.docx'):
            doc = docx.Document(file_path) 
            page_count = len(doc.paragraphs)
            paper_size = doc.sections[0].page_width, doc.sections[0].page_height
        elif file.endswith('.pdf'):
            pdf = PdfFileReader(open(file_path,'rb'))
            page_count = pdf.getNumPages()
            paper_size = pdf.getPage(0).mediaBox[2:], pdf.getPage(0).mediaBox[3:]
        else:
            continue
        
        result = f"{file_path}|{page_count}|{paper_size[0]}x{paper_size[1]}"
        results.append(result)
        
with open('paths.txt', 'w') as f:
    f.write('\n'.join(results))

print('Results exported to paths.txt')

AI 生成的代码,未测试过,仅供参考

免费评分

参与人数 1吾爱币 +1 收起 理由
SU150228 + 1 谢谢您的热心帮助,自己就是不会调试,需要成品

查看全部评分

追随大佬的小弟 发表于 2023-8-28 09:47
要获取Word和PDF文件的页数和纸张大小,需要使用外部工具或库。Python有与Word和PDF交互的库,这些库可以帮助我们获得所需的信息。

这里我为您提供了一个Python脚本来实现您的要求:

1. 使用`python-docx`库获取Word文档的信息。
2. 使用`PyPDF2`库获取PDF文档的信息。

首先,您需要安装必要的库:

```bash
pip install python-docx PyPDF2
```

然后,您可以使用以下Python脚本来执行任务:

```python
import os
from docx import Document
import PyPDF2

def get_word_info(filepath):
    try:
        doc = Document(filepath)
        pages = len(doc.element.xpath('//w:sectPr'))
        # 这里我们假设Word文档的纸张大小为A4
        return pages, "A4"
    except Exception as e:
        return None, None

def get_pdf_info(filepath):
    try:
        with open(filepath, 'rb') as file:
            reader = PyPDF2.PdfFileReader(file)
            pages = reader.numPages
            # 获取纸张大小 (单位:点)
            width, height = reader.getPage(0).mediaBox[2:]
            # 根据大小判断纸张类型
            if width == 595 and height == 842:
                size = "A4"
            else:
                size = f"{width}x{height}"
            return pages, size
    except Exception as e:
        return None, None

def main(folder):
    output = []
    for root, _, files in os.walk(folder):
        for file in files:
            if file.endswith(('.docx', '.doc', '.pdf')):
                filepath = os.path.join(root, file)
                if file.endswith(('.docx', '.doc')):
                    pages, size = get_word_info(filepath)
                elif file.endswith('.pdf'):
                    pages, size = get_pdf_info(filepath)
               
                if pages and size:
                    output.append(f"{filepath};{pages};{size}")

    with open("paths.txt", "w", encoding="utf-8") as f:
        f.write("\n".join(output))

if __name__ == "__main__":
    folder = "D:\\PLDY"
    main(folder)
```

请注意:

- Word文档的页数可能不会100%准确,因为我们是基于分区来估算的。
- PDF的纸张大小我们只做了A4的判断,如果不是A4,我们会输出具体的点数大小。

运行上述Python脚本后,会生成一个`paths.txt`文件,其中包含文件路径、页数和纸张大小,这三项之间使用了`;`作为分隔符。

免费评分

参与人数 1热心值 +1 收起 理由
SU150228 + 1 生成的paths.txt中,测试docx文档28页只统计2页,pdf文档未能统计

查看全部评分

云的彼岸918 发表于 2023-8-28 09:47
这种功能最便捷的就是python
TXT2BIN 发表于 2023-8-28 10:17
楼主需要能原地执行的bat 或者 exe,地址直接写死,就是 bat 或者 exe 文件所在的文件夹,输出也在当前位置就行了
 楼主| SU150228 发表于 2023-8-28 10:48
justfly99 发表于 2023-8-28 09:37
[mw_shl_code=python,true]import os
from pathlib import Path
import docx

生成的paths.txt文档中内容是空的
 楼主| SU150228 发表于 2023-8-29 10:39
烦请路过的大神能给个解决的方案
zpy2 发表于 2023-8-29 13:09
SU150228 发表于 2023-8-29 10:39
烦请路过的大神能给个解决的方案

https://e.anyoupin.cn/Edata/?p=t ... excel/DocxPages.zip

试试这个docx页统计
 楼主| SU150228 发表于 2023-8-29 13:20
zpy2 发表于 2023-8-29 13:09
https://e.anyoupin.cn/Edata/?p=tools.ceshi.index/downloadTreeFile&file=/excel/DocxPages.zip

试 ...

您好,执行时闪退,不知有没有生成的文档
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-1 10:27

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表