|
发表于 2020-7-7 23:16:53
|
显示全部楼层
本帖最后由 hrp 于 2020-7-7 23:55 编辑
重新写了适应表格,你试试
- # coding = utf-8
- # 最终版本
- import os
- import tkinter.messagebox
- import pdfplumber
- realdir = os.path.dirname(os.path.realpath(__file__))
- pdffiles = [
- os.path.join(realdir, f) for f in os.listdir(realdir)
- if os.path.isfile(f) and f[-3:] == 'pdf'
- ]
- for pdffile in pdffiles:
- with pdfplumber.open(pdffile) as pdf:
- # 这里写pdf.pages[0]那么就要保证表格在第一页,否则就会出错。
- first_page = pdf.pages[0]
- tables = first_page.extract_tables()
- strings = (string for table in tables for row in table for string in row)
- new_name = None
- for string in strings:
- index = string.find('WLDD')
- if index != -1:
- new_name = string[index:index + 18]
- break
- if new_name:
- new_path = os.path.join(realdir, new_name + '.pdf')
- try:
- os.rename(pdffile, new_path)
- # 重命名失败则跳过(一般是已存在同名文件)。
- except Exception:
- pass
- tkinter.messagebox.showinfo("提示", "重命名已完成")
复制代码 |
|