这个博客荒废了很久,从当时刚进课题组到现在博都毕业了,读这几年书老以自己忙为借口,今天才又提笔。
最近新型冠状肺炎很严重,在家没事就干活,最近遇到一个问题,写程序解决了。就在这里记录一下。
最近在投文章,但是你懂的,插入了Endnote格式以后,等到最终定稿,你把带有Endnote链接的格式转换成plain text格式(也就是不会链接到Endnote的格式,即无法用Endnote修改了成为纯word文本)发给老师,老师这边今天补一点明天补一点,甚至直接删,还不止一个老师,有时根本就来不及在你的原来endnote版本上改完。。。最后 等到你要重投文章改格式或者大批量改文献时,你就要一!个!一!个!文献的重新插一遍!!!(想死的心都有。。。)
网上搜索:endnote最终改为text格式的文档,还能倒回到endnote吗?变成可编辑文本后还可以变回endnote格式么?EndNote plain text可以和EndNote重新建立关联吗?所有都回答No。。。
于是,怎么办?不想做苦力,能不能用程序帮我们减少工作量?我观察了一下Endnote插入的最初格式(即Endnote会识别的格式)为:
#举例:{Betts, 2018 #83}
#解释一下,分别是第一作者的姓,文章年份,文章在你本地endnote的Record Number(就文章唯一序号)
那这就简单了,想办法拿到这个Record Number序号,生成这个格式,插入到word文中对应位置就可以了。
先说结果,写了一个简单的程序,发布在Github上:https://github.com/YiyanYang0728/Insert_Endnote_format
看一下Readme.md,用法写的还行,运行get_endnote_insert_fmt.py就行。
也贴出代码:
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 9 10:36:06 2020
@author: LucasTsubasaYang
"""
"""get Endnote insert format"""
import re
def get_Endnote_insert_format(endnote_file, paper_ref_file, outfile):
"""input files: endnote_file, paper_ref_file
outputfile: outfile
"""
###Read Endnote library references
endnote_ref = {}
f = open(endnote_file, 'r', encoding='utf-8')
items_in_need = ['Author','Year','Title']
for line in f:
if line.startswith('Record Number'):
rec_no = line.strip().split(': ')[1]
# print(rec_no)
endnote_ref[rec_no] = {'Author':'NULL','Year':'NULL','Title':'NULL'}
for key in items_in_need:
if line.startswith(key+': '):
content = line.strip().split(': ',1)[1]
endnote_ref[rec_no][key] = content
f.close()
# Here you can check the information of a given Record Number
# recno = '80'
# print(endnote_ref[recno]['Title'][:30], endnote_ref[recno]['Year'], endnote_ref[recno]['Author'].split(',')[0])
###Read your references list in your plain-text paper
paper_ref = {}
f = open(paper_ref_file, 'r')
for line in f:
No = line.split('\t')[0].split('.')[0]
ref = line.split('\t')[1]
lst = [i.strip() for i in re.split('\(|\)', ref, maxsplit=2)]
author, year, title = lst[:3]
title = title.split('. ')[0]
paper_ref[No] = [author, year, title]
f.close()
###Write Endnote insert format into outfile
g = open(outfile, 'w')
for i in sorted(map(int, paper_ref.keys())):
No = str(i)
print(No)
if No not in paper_ref:
print('This No. not exist!')
continue
#Match to find Endnote Record Number
for rec_no in endnote_ref:
query_title = paper_ref[No][2].lower()[:30]
query_year = paper_ref[No][1]
query_author = ' '.join(re.split('\,|\&', paper_ref[No][0])[0].split()[:-1])
if endnote_ref[rec_no]['Title'][:30].lower() == query_title \
and endnote_ref[rec_no]['Year'] == query_year \
and endnote_ref[rec_no]['Author'].split(',')[0] == query_author:
#FORMAT example: {Betts, 2018 #83}
year = query_year
author_last_name = query_author
print(query_title, query_year, query_author)
insert_format = '{'+author_last_name+', '+year+' #'+rec_no+'}'
print(insert_format)
g.write(No+'. '+'|'.join([query_title, query_year, query_author])+'\n')
g.write(No+'. '+insert_format+'\n')
break
else:
#Not find this paper's record number
print(No, paper_ref[No])
print("Not find this reference's Record Number!")
print(paper_ref[No][2].lower()[:30], paper_ref[No][1], \
paper_ref[No][0].split(',')[0].split()[0])
g.close()
if __name__ == '__main__':
endnote_file = r'Example_input_exported_endnote_style.txt'
paper_ref_file = r'Example_input_paper_reference_list.txt'
outfile = r'Example_output_insert_format.txt'
get_Endnote_insert_format(endnote_file, paper_ref_file, outfile)
下面说下思路,毕竟写的很naive,只认文章里类似PNAS的格式。。。如果你是别的格式,你就要在下面部分做改动,让程序读出你文章里每条reference的作者、年份和名字。【这个我之后会尽量完善的。。。】
paper_ref = {}
f = open(paper_ref_file, 'r')
for line in f:
No = line.split('\t')[0].split('.')[0]
ref = line.split('\t')[1]
lst = [i.strip() for i in re.split('\(|\)', ref, maxsplit=2)]
author, year, title = lst[:3]
title = title.split('. ')[0]
paper_ref[No] = [author, year, title]
f.close()
我们人工重新插入文章,就同时看三件事,1. 文章名字; 2. 作者(一作就行);3. 年份
这三个都一样的话,99%是同一篇文献了。
首先我们拿到输入文件,来自Endnote你本地的库,操作打开Endnote X9(我用X9),Open Endnote->Select all the references in your endnote (选中你的所有文献,太多的话就你需要插入的所有文献,选中为蓝色)->File->Export->Save type: Text file 且 Output style: show all fields->Save,得到一个txt,叫A,里面这样
Reference Type: Book
Record Number: 109
Author: Darwin, Charles
Year: 1888
Title: On the origin of species by means of natural selection: or the preservation of favored races in the struggle for life
Publisher: D. Appleton
Volume: 2
Short Title: On the origin of species by means of natural selection: or the preservation of favored races in the struggle for life
这里就有Record Number
然后你把你word底部的文章列表粘贴出来得到另一个txt,叫B。我这里长这样:
1. Karner MB, Delong EF, & Karl DM (2001) Archaeal dominance in the mesopelagic zone of the Pacific Ocean. Nature 409(6819):507-510.
2. DeLong EF (1992) Archaea in coastal marine environments. Proc Natl Acad Sci U S A 89(12):5685-5689.
你把A和B里面的文章作者、年份、标题程序比对,从A中查到Record Number再改造下就得到了Endnote格式,多Easy!!!
结果如下:
1. archaeal dominance in the meso|2001|Karner【这是给你check用的】
1. {Karner, 2001 #26}【这是可以用的Endnote格式】
2. archaea in coastal marine envi|1992|DeLong
2. {DeLong, 1992 #15}
后一步就比较傻了,你进word根据文献序号一个一个看上面的表复制粘贴到word对应处就行。【这一步是太傻了,关于这点我也在改进,看到python-docx可以直接编辑word,之后会想要改进这个功能,争取可以直接在word中修改】
最后为了让文献生效记得使用Endnote的Update Citations&Bibliography功能。