已导出为可执行文件
下载链接在评论区
此为改进版
解决了期刊名可能是最大字体的问题:
思路 识别字符串的空格>3则为标题
import os
import PyPDF2import fitzdef find_largest_font_sentence(pdf_path):largest_font_size = 0largest_font_sentence = ''maxsize=0# 打开PDF文件document = fitz.open(pdf_path)for page_number in range(document.page_count):page = document.load_page(page_number)blocks = page.get_text("dict")["blocks"]for block in blocks:if "lines" in block: # 检查是否存在 lines 字段for line in block["lines"]:for span in line["spans"]:if span["size"] > largest_font_size:largest_font_size = span["size"]largest_font_sentence = span["text"]maxsize=largest_font_sizefor block in blocks:if "lines" in block: # 检查是否存在 lines 字段for line in block["lines"]:for span in line["spans"]:if span["size"] ==maxsize:if largest_font_sentence!=span["text"]:largest_font_sentence = largest_font_sentence+' '+span["text"]if largest_font_sentence.count(' ')<4:largest_font_sentence=''largest_font_size=0for block in blocks:if "lines" in block: # 检查是否存在 lines 字段for line in block["lines"]:for span in line["spans"]:if span["size"] > largest_font_size and span["text"].count(' ')>3:largest_font_size = span["size"]largest_font_sentence = span["text"]maxsize = largest_font_sizefor block in blocks:if "lines" in block: # 检查是否存在 lines 字段for line in block["lines"]:for span in line["spans"]:if span["size"] == maxsize:if largest_font_sentence != span["text"]:largest_font_sentence = largest_font_sentence + ' ' + span["text"]return largest_font_sentence# 用于测试的PDF文件路径def rename_pdfs(directory):for filename in os.listdir(directory):if filename.endswith(".pdf"):filepath = os.path.join(directory, filename)filepath=filepath.strip()largest_font_sentence = find_largest_font_sentence(filepath)title=largest_font_sentenceif ":" in title:title = title.replace(":", ":")new_filename = title.strip() + ".pdf"new_filepath = os.path.join(directory, new_filename)os.rename(filepath, new_filepath)print(f"Renamed {filename} to {new_filename}")# 指定包含英文论文PDF的目录路径
pdf_directory = "PDF文件目录"# 调用函数进行重命名
rename_pdfs(pdf_directory)
再次更新
import os
import time
import PyPDF2
import fitz
def find_largest_font_sentence(pdf_path):
largest_font_size = 0largest_font_sentence = ''maxsize=0# 打开PDF文件document = fitz.open(pdf_path)for page_number in range(1):page = document.load_page(page_number)blocks = page.get_text("dict")["blocks"]for block in blocks:if "lines" in block: # 检查是否存在 lines 字段for line in block["lines"]:for span in line["spans"]:if span["size"] > largest_font_size:largest_font_size = span["size"]largest_font_sentence = span["text"]maxsize=largest_font_sizefor block in blocks:if "lines" in block: # 检查是否存在 lines 字段for line in block["lines"]:for span in line["spans"]:if span["size"] ==maxsize:if largest_font_sentence!=span["text"]:largest_font_sentence = largest_font_sentence+' '+span["text"]if largest_font_sentence.count(' ')<4 or "arXiv" in largest_font_sentence:largest_font_sentence=''largest_font_size=0for block in blocks:if "lines" in block: # 检查是否存在 lines 字段for line in block["lines"]:for span in line["spans"]:if span["size"] > largest_font_size and span["text"].count(' ')>3 and "arXiv" not in span["text"]:largest_font_size = span["size"]largest_font_sentence = span["text"]maxsize = largest_font_sizefor block in blocks:if "lines" in block: # 检查是否存在 lines 字段for line in block["lines"]:for span in line["spans"]:if span["size"] == maxsize:if largest_font_sentence != span["text"]:largest_font_sentence = largest_font_sentence + ' ' + span["text"]return largest_font_sentence
用于测试的PDF文件路径
def rename_pdfs(directory):
for filename in os.listdir(directory):if filename.endswith(".pdf"):filepath = os.path.join(directory, filename)filepath=filepath.strip()largest_font_sentence = find_largest_font_sentence(filepath)title=largest_font_sentenceif ":" in title:title = title.replace(":", ":")new_filename = title.strip() + ".pdf"new_filepath = os.path.join(directory, new_filename)os.rename(filepath, new_filepath)print(f"Renamed {filename} to {new_filename}")
指定包含英文论文PDF的目录路径
#比如pdf文件在 D:\学习\论文
#那么引号内需要输入的是 D:\学习\论文
print(“请输入论文的存放目录”)
a=input(“输入路径”)
a=a.replace(“\”,“/”)
pdf_directory = a
调用函数进行重命名
rename_pdfs(pdf_directory)
time.sleep(5)