# -*- coding=utf-8 -*-
"""main方法
"""
__author__ = ""
import gui
def main():
gui.gui_main()
if __name__ == "__main__":
main()
# -*- coding=utf-8 -*-
"""用户界面"""
__author__ = ""
import os
import tkinter
from tkinter import filedialog
from tkinter import StringVar
import docx
import docx_process
import traceback
# 输入/输出文档路径
file_input_path = None
file_output_path = None
# click chooseFile button
def choose_input_file(file_input_entry):
global file_input_path
file_input_path = tkinter.filedialog.askopenfilename()
input_variable.set(file_input_path)
# click choose output path button
def choose_output_path(file_output_entry):
global file_output_path
file_output_path = tkinter.filedialog.askdirectory()
output_variable.set(file_output_path)
# 提交路径开始处理文档
def submit(file_input_path):
try:
# 读取文档
document = docx_process.create_document(file_input_path)
# 参考文献索引排序
docx_process.index_sort(document)
# 参考文献列表排序
docx_process.quote_sort(document)
# 保存到输出路径
docx_process.docx_save(document, file_output_path + "/结果.docx")
# 自动打开输出路径
os.startfile(file_output_path)
# 替换提示语
result_variable.set("已完成, 已为你打开输出文件夹")
except docx.opc.exceptions.PackageNotFoundError:
result_variable.set("错误! 请检查输入文件以及格式(.docx)是否正确!")
except TypeError:
result_variable.set("错误! 请检查输出路径是否存在!!")
except PermissionError:
result_variable.set("错误! 输出目录权限不足, 或请检查输出目录是否有同名文件正在使用, 请关闭后重试!!")
except BaseException:
result_variable.set("未知错误!!")
else:
result_variable.set("已完成, 已为你打开输出文件夹")
def gui_main():
global file_input_path
global file_output_path
window = tkinter.Tk()
window.title("Word文档参考文献自动排序工具")
window.geometry("600x400")
# 文档路径变量
# input_path_variable = tkinter.StringVar()
# input_path_variable.set("")
# output_path_variable = tkinter.StringVar()
# output_path_variable.set("")
## 输入
input_label_frame = tkinter.LabelFrame(window, text="选择文档")
input_label_frame.pack(fill=tkinter.Y, side=tkinter.TOP, padx=5, pady=15, ipadx=5, ipady=15)
# 选择文件标签
file_input_label = tkinter.Label(input_label_frame, text=" 原文档路径: ")
file_input_label.pack(side=tkinter.LEFT)
# 文件路径输入框
global input_variable
input_variable = StringVar()
input_variable.set("请在此处填写需要修改的文档路径")
file_input_entry = tkinter.Entry(input_label_frame, textvariable=input_variable, width=50)
file_input_entry.pack(side=tkinter.LEFT, padx=5)
# 选择输入文件按钮
file_input_choose_button = tkinter.Button(input_label_frame, text="选择文件", width=20, \
command=lambda: choose_input_file(file_input_entry))
file_input_choose_button.pack(side=tkinter.LEFT, padx=5)
## 输出
output_label_frame = tkinter.LabelFrame(window, text="处理结果")
output_label_frame.pack(fill=tkinter.Y, side=tkinter.TOP, padx=5, pady=15, ipadx=5, ipady=15)
# 选择输出文件标签
file_output_label = tkinter.Label(output_label_frame, text="结果输出路径:")
file_output_label.pack(side=tkinter.LEFT)
# 文件路径输出框
global output_variable
output_variable = StringVar()
output_variable.set("请在此处填写文件的输出目录")
file_output_entry = tkinter.Entry(output_label_frame, textvariable=output_variable, width=50)
file_output_entry.pack(side=tkinter.LEFT, padx=5)
# 选择输出文件按钮
file_output_choose_button = tkinter.Button(output_label_frame, text="选择输出文件路径", width=20, \
command=lambda: choose_output_path(file_output_entry))
file_output_choose_button.pack(side=tkinter.LEFT, padx=5)
# submit Button
file_input_submit_button = tkinter.Button(window, text="开始执行", font=30, width=20,height=2, \
command=lambda: submit(file_input_path))
file_input_submit_button.pack(side=tkinter.TOP, padx=5)
# 结果标签
global result_variable
result_variable = StringVar()
result_variable.set("请选择输入/输出路径, 执行成功后会在输出路径生成名为'结果.docx的文档'")
result_label = tkinter.Label(window, textvariable=result_variable)
result_label.pack(side=tkinter.TOP, padx=5, pady=15)
more_func_notic_label_frame = tkinter.LabelFrame(window, text="更多功能")
more_func_notic_label_frame.pack(side=tkinter.BOTTOM, pady=15)
more_func_label1 = tkinter.Label(more_func_notic_label_frame, text="更多功能正在开发中, 欢迎提出宝贵意见!", width=80)
more_func_label1.pack()
more_func_label2 = tkinter.Label(more_func_notic_label_frame, text="QQ: 1739473807", width=80)
more_func_label2.pack()
# 开始循环窗口
window.mainloop()
if __name__ == '__main__':
gui_main()
# -*- coding=utf-8 -*-
"""文档处理
"""
__author__ = ""
from docx import Document
import re
import copy
# 正文索引字典
indexDir = {}
# 参考文献字典
quotesDir = {}
# 创建文档对象
def create_document(path):
document = Document(path)
return document
# 给正文中的索引重新排序
def index_sort(document):
global indexDir
# 设定一个num, 记录是第几个参考文献引用
num = 1
for paragraph in document.paragraphs:
# 获取正文中的引用
indexs = re.findall(r"(\w)+(\[\d+\])", paragraph.text)
if len(indexs) >= 1:
# 获取本段的字体
font_size = paragraph.runs[0].font.size
font_name = paragraph.runs[0].font.name
# 判断这一段中的引用个数是不是>1, 如果>1, 则逐句拆开进行修改
if len(indexs) == 1:
# 获取原文中引用的标号, 这里是提取中括号中的标号
indexNum = indexs[0][1][1:-1]
# 修改前的标号
old = "[" + str(indexNum) + "]"
# 修改后的标号
new = "[" + str(num) + "]"
# 替换
paragraph.text = paragraph.text.replace(old, new)
# 将更新好的段落添加到字典
indexDir[indexNum] = paragraph
num += 1
elif len(indexs) > 1:
sentenses = paragraph.text.split(",") # 如果是英文,注意使用英文标点
text = ""
for sentense in sentenses:
if re.search(r"(\w)+(\[\d+\])", sentense):
partern = re.compile("\[\d\]")
new = "[" + str(num) + "]"
sentense = partern.sub(new, sentense)
num += 1
text = text + "," + sentense
paragraph.text = text
for index in indexs:
indexNum = index[1][1:-1]
indexDir[indexNum] = paragraph
# 修改字体
repair_font_size(paragraph, font_name, font_size)
## 给文末的参考文献排序
def quote_sort(document):
global indexDir
global quotesDir
# 字体
font_size = None
font_name = None
# 遍历段落, 查找参考文献列表
for p in document.paragraphs:
flag = re.match(r"^(\[\d+\])(\w)+", p.text)
if flag:
quotesDir[flag.group()[1:-1]] = p
font_name = p.runs[0].font.name
font_size = p.runs[0].font.size
# 给参考文献排序
quotesDirCopy = copy.deepcopy(quotesDir)
# 设定一个num, 记录参考文献原本的序号
num = 1
# 遍历正文索引, 找到对应的参考文献, 进行标号替换
for indexKey in indexDir:
quotesDir[str(num)].text = quotesDirCopy[indexKey].text
quotesDir[str(num)].text = re.sub("\[\d\]", "[" + str(num) + "]", quotesDir[str(num)].text)
num += 1
# 删除多余的索引, 并且按照参考文献标号调整顺序
quotesDirCopy = copy.deepcopy(quotesDir)
num = 1
for key in quotesDir:
if int(key) > len(quotesDir):
quotesDir[key].text = ""
else:
quotesDir[key].text = quotesDirCopy[str(num)].text
num += 1
# 改变字体
repair_font_size(quotesDir[key], font_name, font_size)
# 保存文档
def docx_save(document, save_path):
document.save(save_path)
# 修改字体
def repair_font_size(paragraph, font_name, font_size):
# 遍历每个run, 更新为原文的字体和字号
for run in paragraph.runs:
run.font.name = font_name
run.font.size = font_size
"""main方法
"""
__author__ = ""
import gui
def main():
gui.gui_main()
if __name__ == "__main__":
main()
# -*- coding=utf-8 -*-
"""用户界面"""
__author__ = ""
import os
import tkinter
from tkinter import filedialog
from tkinter import StringVar
import docx
import docx_process
import traceback
# 输入/输出文档路径
file_input_path = None
file_output_path = None
# click chooseFile button
def choose_input_file(file_input_entry):
global file_input_path
file_input_path = tkinter.filedialog.askopenfilename()
input_variable.set(file_input_path)
# click choose output path button
def choose_output_path(file_output_entry):
global file_output_path
file_output_path = tkinter.filedialog.askdirectory()
output_variable.set(file_output_path)
# 提交路径开始处理文档
def submit(file_input_path):
try:
# 读取文档
document = docx_process.create_document(file_input_path)
# 参考文献索引排序
docx_process.index_sort(document)
# 参考文献列表排序
docx_process.quote_sort(document)
# 保存到输出路径
docx_process.docx_save(document, file_output_path + "/结果.docx")
# 自动打开输出路径
os.startfile(file_output_path)
# 替换提示语
result_variable.set("已完成, 已为你打开输出文件夹")
except docx.opc.exceptions.PackageNotFoundError:
result_variable.set("错误! 请检查输入文件以及格式(.docx)是否正确!")
except TypeError:
result_variable.set("错误! 请检查输出路径是否存在!!")
except PermissionError:
result_variable.set("错误! 输出目录权限不足, 或请检查输出目录是否有同名文件正在使用, 请关闭后重试!!")
except BaseException:
result_variable.set("未知错误!!")
else:
result_variable.set("已完成, 已为你打开输出文件夹")
def gui_main():
global file_input_path
global file_output_path
window = tkinter.Tk()
window.title("Word文档参考文献自动排序工具")
window.geometry("600x400")
# 文档路径变量
# input_path_variable = tkinter.StringVar()
# input_path_variable.set("")
# output_path_variable = tkinter.StringVar()
# output_path_variable.set("")
## 输入
input_label_frame = tkinter.LabelFrame(window, text="选择文档")
input_label_frame.pack(fill=tkinter.Y, side=tkinter.TOP, padx=5, pady=15, ipadx=5, ipady=15)
# 选择文件标签
file_input_label = tkinter.Label(input_label_frame, text=" 原文档路径: ")
file_input_label.pack(side=tkinter.LEFT)
# 文件路径输入框
global input_variable
input_variable = StringVar()
input_variable.set("请在此处填写需要修改的文档路径")
file_input_entry = tkinter.Entry(input_label_frame, textvariable=input_variable, width=50)
file_input_entry.pack(side=tkinter.LEFT, padx=5)
# 选择输入文件按钮
file_input_choose_button = tkinter.Button(input_label_frame, text="选择文件", width=20, \
command=lambda: choose_input_file(file_input_entry))
file_input_choose_button.pack(side=tkinter.LEFT, padx=5)
## 输出
output_label_frame = tkinter.LabelFrame(window, text="处理结果")
output_label_frame.pack(fill=tkinter.Y, side=tkinter.TOP, padx=5, pady=15, ipadx=5, ipady=15)
# 选择输出文件标签
file_output_label = tkinter.Label(output_label_frame, text="结果输出路径:")
file_output_label.pack(side=tkinter.LEFT)
# 文件路径输出框
global output_variable
output_variable = StringVar()
output_variable.set("请在此处填写文件的输出目录")
file_output_entry = tkinter.Entry(output_label_frame, textvariable=output_variable, width=50)
file_output_entry.pack(side=tkinter.LEFT, padx=5)
# 选择输出文件按钮
file_output_choose_button = tkinter.Button(output_label_frame, text="选择输出文件路径", width=20, \
command=lambda: choose_output_path(file_output_entry))
file_output_choose_button.pack(side=tkinter.LEFT, padx=5)
# submit Button
file_input_submit_button = tkinter.Button(window, text="开始执行", font=30, width=20,height=2, \
command=lambda: submit(file_input_path))
file_input_submit_button.pack(side=tkinter.TOP, padx=5)
# 结果标签
global result_variable
result_variable = StringVar()
result_variable.set("请选择输入/输出路径, 执行成功后会在输出路径生成名为'结果.docx的文档'")
result_label = tkinter.Label(window, textvariable=result_variable)
result_label.pack(side=tkinter.TOP, padx=5, pady=15)
more_func_notic_label_frame = tkinter.LabelFrame(window, text="更多功能")
more_func_notic_label_frame.pack(side=tkinter.BOTTOM, pady=15)
more_func_label1 = tkinter.Label(more_func_notic_label_frame, text="更多功能正在开发中, 欢迎提出宝贵意见!", width=80)
more_func_label1.pack()
more_func_label2 = tkinter.Label(more_func_notic_label_frame, text="QQ: 1739473807", width=80)
more_func_label2.pack()
# 开始循环窗口
window.mainloop()
if __name__ == '__main__':
gui_main()
# -*- coding=utf-8 -*-
"""文档处理
"""
__author__ = ""
from docx import Document
import re
import copy
# 正文索引字典
indexDir = {}
# 参考文献字典
quotesDir = {}
# 创建文档对象
def create_document(path):
document = Document(path)
return document
# 给正文中的索引重新排序
def index_sort(document):
global indexDir
# 设定一个num, 记录是第几个参考文献引用
num = 1
for paragraph in document.paragraphs:
# 获取正文中的引用
indexs = re.findall(r"(\w)+(\[\d+\])", paragraph.text)
if len(indexs) >= 1:
# 获取本段的字体
font_size = paragraph.runs[0].font.size
font_name = paragraph.runs[0].font.name
# 判断这一段中的引用个数是不是>1, 如果>1, 则逐句拆开进行修改
if len(indexs) == 1:
# 获取原文中引用的标号, 这里是提取中括号中的标号
indexNum = indexs[0][1][1:-1]
# 修改前的标号
old = "[" + str(indexNum) + "]"
# 修改后的标号
new = "[" + str(num) + "]"
# 替换
paragraph.text = paragraph.text.replace(old, new)
# 将更新好的段落添加到字典
indexDir[indexNum] = paragraph
num += 1
elif len(indexs) > 1:
sentenses = paragraph.text.split(",") # 如果是英文,注意使用英文标点
text = ""
for sentense in sentenses:
if re.search(r"(\w)+(\[\d+\])", sentense):
partern = re.compile("\[\d\]")
new = "[" + str(num) + "]"
sentense = partern.sub(new, sentense)
num += 1
text = text + "," + sentense
paragraph.text = text
for index in indexs:
indexNum = index[1][1:-1]
indexDir[indexNum] = paragraph
# 修改字体
repair_font_size(paragraph, font_name, font_size)
## 给文末的参考文献排序
def quote_sort(document):
global indexDir
global quotesDir
# 字体
font_size = None
font_name = None
# 遍历段落, 查找参考文献列表
for p in document.paragraphs:
flag = re.match(r"^(\[\d+\])(\w)+", p.text)
if flag:
quotesDir[flag.group()[1:-1]] = p
font_name = p.runs[0].font.name
font_size = p.runs[0].font.size
# 给参考文献排序
quotesDirCopy = copy.deepcopy(quotesDir)
# 设定一个num, 记录参考文献原本的序号
num = 1
# 遍历正文索引, 找到对应的参考文献, 进行标号替换
for indexKey in indexDir:
quotesDir[str(num)].text = quotesDirCopy[indexKey].text
quotesDir[str(num)].text = re.sub("\[\d\]", "[" + str(num) + "]", quotesDir[str(num)].text)
num += 1
# 删除多余的索引, 并且按照参考文献标号调整顺序
quotesDirCopy = copy.deepcopy(quotesDir)
num = 1
for key in quotesDir:
if int(key) > len(quotesDir):
quotesDir[key].text = ""
else:
quotesDir[key].text = quotesDirCopy[str(num)].text
num += 1
# 改变字体
repair_font_size(quotesDir[key], font_name, font_size)
# 保存文档
def docx_save(document, save_path):
document.save(save_path)
# 修改字体
def repair_font_size(paragraph, font_name, font_size):
# 遍历每个run, 更新为原文的字体和字号
for run in paragraph.runs:
run.font.name = font_name
run.font.size = font_size