识别pdf中论文标题并重命名PDF名称(2024.1.2,第二次更新)判断标题中是否以空格结尾

发布时间:2024年01月02日

63~66行增加语句,判断标题是否以空格结尾
83~85行增加语句,判断选句是否以空格结尾

import os
import time




import fitz



def find_largest_font_sentence(pdf_path):

    largest_font_size = 0

    largest_font_sentence = ''

    maxsize=0

    # 打开PDF文件

    document = fitz.open(pdf_path)



    for page_number in range(2):

        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_size

        for 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"]

#判断largest_font_sentence是否结尾含有空格
#如果有则删掉
        if largest_font_sentence.endswith(' '):
            largest_font_sentence = largest_font_sentence.rstrip()


        if largest_font_sentence.count(' ')<4 or "arXiv" in largest_font_sentence:

            largest_font_sentence=''

            largest_font_size=0

            for block in blocks:

                if "lines" in block:  # 检查是否存在 lines 字段

                    for line in block["lines"]:

                        for span in line["spans"]:
##判定句子中空格的个数如果大于3就认为它是标题而非期刊名称
##判断句子是否以空格结尾,如果是就删掉空格
                            if span["text"].endswith(''):
                                span["text"] = span["text"].rstrip()

                            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_size

            for 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):

    failed_files = []  # 记录重命名失败的文件

    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_sentence

            if  ":" in title:

                title = title.replace(":", ":")

            new_filename = title.strip() + ".pdf"

            new_filepath = os.path.join(directory, new_filename)

            try:
                os.rename(filepath, new_filepath)
            except FileNotFoundError:
                failed_files.append(filename)

    if len(failed_files) > 0:
        if len(failed_files) > 0:
            print(f"重命名失败 {len(failed_files)} 个pdf文件:")
            for file in failed_files:
                print(file)


# 指定包含英文论文PDF的目录路径

#比如pdf文件在 D:\学习\论文

#那么引号内需要输入的是 D:\\学习\\论文
print("请输入论文的存放目录")
a=input("输入路径")
a=a.replace("\\","/")
pdf_directory = a





# 调用函数进行重命名

rename_pdfs(pdf_directory)
input("按下回车键可关闭窗口")
文章来源:https://blog.csdn.net/weixin_44751432/article/details/135345424
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。