python代码如下:
import docx
from docx import Document
import re
# 排序后的字符串插入word
def insert_to_word(text, filename):
doc = docx.Document(filename)
paragraphs = doc.paragraphs
paragraphs[-1].text +="\n" + text
doc.save(filename)
#获取原文引用参考文献的次序字典
def get_index_order(docx_file):
# 读取word文档
document = Document(docx_file)
# 将所有段落的文本内容合并成一个字符串
text = '\n'.join([para.text for para in document.paragraphs])
# 使用正则表达式匹配方括号中的数字
pattern = re.compile(r'\[(\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*?)\]')
matches = pattern.finditer(text)
# 处理匹配结果,记录先后次序
orders = {}
for match in matches:
index_str = match.group(1)
# 将字符串转换为数字列表
indices = []
for part in index_str.split(','):
if '-' not in part:
indices.append(int(part))
else: