python实现endnote文献批量关联PDF

endnote直接导入文献的PDF可以直接识别到期刊、作者等相关信息,但是部分文件会出现识别出错的情况,中文文献就更不用说了。都得自己先下载引文格式,再去一个个的关联本地的PDF文件。为此老白写了一个python的脚本,可以实现在已经存在endnote library情况下,批量关联本地的pdf文件。详细说明如下:

python实现endnote文献批量关联PDF

1.endnote导出原library库

在endnote软件左上角,点击导出,选择xml格式(参考文献格式自选)

python实现endnote文献批量关联PDF

2.endnote关联文件代码解读

从xml文件里面,我们可以分析下面两种代码

有文献关联文件的特征代码

<urls><pdf-urls><url>file://{}</url></pdf-urls></urls>

无文献关联文件特征代码

<urls></urls>
实现思路
我们可以简单的通过搜索替换的方式,将没有关联文献的替换为关联了文献的即可

3.python脚本编写

脚本如下,其中keywords.txt就是存放你的文献的位置的,456.xml是导出的endnote库

import re

def replace_urls(xml_file_path):
    replacements = []
    
    # 从文件中读取关键词并添加到替换列表中
    keywords_file_path = r"C:Usersxcbtmw.comDesktopkeywords.txt"
    with open(keywords_file_path, 'r', encoding='utf-8') as file:
        for line in file:
            keyword = line.strip()
            replacements.append(keyword)

    count = 0

    def replace(match):
        nonlocal count
        replacement = replacements[count]
        count += 1
        return "<urls><pdf-urls><url>file://{}</url></pdf-urls></urls>".format(replacement)

    with open(xml_file_path, 'r', encoding='utf-8') as file:
        xml_content = file.read()

    pattern = r"<urls></urls>"
    result = re.sub(pattern, replace, xml_content)
    
    # 新建结果文件并保存替换后的内容
    result_file_path = r"C:Usersxcbtmw.comDesktopresult.xml"
    with open(result_file_path, 'w', encoding='utf-8') as file:
        file.write(result)

    print("替换后的XML已保存至:" + result_file_path)

xml_file_path = r"C:Usersxcbtmw.comDesktop456.xml"

replace_urls(xml_file_path)

由于保存文件名可能不一致,keywords.txt需要自行填入,如果你的文件都是在同一个文件夹下,可以先获取所有文件名,再统一构造路径即可。

如果不在,那么建议使用everything搜索工具获取。keywords.txt文件示例如下:

F:资料工作备份xcbtmw.com你的文件路径sdf.pdf
F:资料工作备份xcbtmw.com你的文件路径xml.pdf
F:资料工作备份xcbtmw.com你的文件路径mxf.pdf

4.导入result.xml

填写好对应的文件位置和内容后,就生成了result.xml文件,再导入到endnote即可。

5.个人建议

把所有的文献移动到一个文件夹下,路径就可以批量获取了。

D:DowloadexploreA_comparison_of_the_application_of_block_theory_and_3D_block_cutting_analysis.pdf
D:DowloadexploreA_fast_mesh_model_for_block_generation_in_tunnels.pdf
D:DowloadexploreA_novel_approach_to_investigating_3D_fracture_connectivity_in_ultrahigh_steep_rock_slopes.pdf
D:DowloadexploreComparison_of_rock_discontinuity_mean_trace_length_and_density_estimation_methods_using_discontinuity_data_from_an_outcrop_in_Wenchuan_area__China.pdf

python实现endnote文献批量关联PDF