本工具基于python开发,格式基于共青团宁夏理工学院计算机科学与工程学院第六届委员会发布的《宁夏理工学院计算机科学与工程学院关于规范公文格式的通知》。
GUI代码如下:
'''
Date: 2022-11-21 14:21:35
LastEditors: Qianshanju
E-mail: z1939784351@gmail.com
LastEditTime: 2022-11-21 17:25:18
FilePath: \HERO\ComputerScience\Program\PythonProgram\SourceCode\document format\GUI.py
'''
import PySimpleGUI as sg
import correct
import webbrowser
layout = [
[sg.Text("本软件可自动将公文转换成相应格式。")],
[sg.Text("仅可转换公文标题、一到四级标题、正文,其余需手动修改。")],
[sg.Text('请选择文件:')],
[sg.Input('', key="_FILE_",),
sg.FileBrowse(button_text='选择文档')],
[sg.Text('请选择大标题的行数:')],
[sg.Radio('1', "title", k='1'),
sg.Radio('2', "title", k='2'),
sg.Radio('3', "title", k='3')],
[sg.Checkbox(text="是否转换为PDF格式?", key="pdf", default=False)],
[sg.Button("确定"), sg.Button("关于本软件")]
]
window = sg.Window('公文格式检查器', layout)
while True:
event, values = window.read()
if event == None:
break
if event == '确定':
if(values['选择文档'].split('.')[1] != 'docx'):
sg.popup_ok("请打开一个后缀名为'.docx'的文档")
else:
if(values['1']):
lines = 1
if(values['2']):
lines = 2
if(values['3']):
lines = 3
if(correct.correct(values['选择文档'], lines, values['pdf'])):
sg.popup_ok("转换成功!")
if(values['选择文档'].split('.')[1] != 'docx'):
sg.popup_ok("请打开一个后缀名为'.docx'的文档")
if event == '关于本软件':
url = 'http://t.csdn.cn/AQIzF'
webbrowser.open(url, new=0, autoraise=True)
window.close()
后端代码如下:
'''
Date: 2022-11-21 14:21:59
LastEditors: Qianshanju
E-mail: z1939784351@gmail.com
LastEditTime: 2022-11-21 16:05:43
FilePath: \JavaPrograme:\HERO\ComputerScience\Program\PythonProgram\SourceCode\document format\check.py
'''
import docx
from docx.shared import Pt
from docx.oxml.ns import qn
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from win32com.client import constants, gencache
def correct(path, title_lines, pdf):
lines = 1
file = docx.Document(path)
print(path[0:-5]+'_new.docx')
for para in file.paragraphs:
para.paragraph_format.line_spacing = Pt(28)
para.paragraph_format.space_before = Pt(0)
para.paragraph_format.space_after = Pt(0)
para.alignment = WD_PARAGRAPH_ALIGNMENT.JUSTIFY
for para in file.paragraphs:
if(lines <= title_lines):
para.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
for run in para.runs:
run.font.size = Pt(22)
run.font.name = '方正小标宋简体'
run._element.rPr.rFonts.set(qn('w:eastAsia'), '方正小标宋简体')
else:
if(para.text[0:2] in ['一、', '二、', '三、', '四、', '五、', '六、', '七、', '八、', '九、']):
for run in para.runs:
run.font.size = Pt(16)
run.font.name = '黑体'
run._element.rPr.rFonts.set(qn('w:eastAsia'), '黑体')
elif(para.text[0:3] in ['(一)', '(二)', '(三)', '(四)', '(五)', '(六)', '(七)', '(八)', '(九)']):
for run in para.runs:
run.font.size = Pt(16)
run.font.bold = True
run.font.name = 'Times New Roman'
run._element.rPr.rFonts.set(qn('w:eastAsia'), '楷体')
elif(para.text[0:2] in ['1.', '2.', '3.', '4.', '5.', '6.', '7.', '8.', '9.']):
para.paragraph_format.first_line_indent = Pt(32)
for run in para.runs:
run.font.size = Pt(16)
run.font.bold = True
run.font.name = 'Times New Roman'
run._element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
elif(para.text[0:3] in ['(1)', '(2)', '(3)', '(4)', '(5)', '(6)', '(7)', '(8)', '(9)']):
para.paragraph_format.first_line_indent = Pt(32)
for run in para.runs:
run.font.size = Pt(16)
run.font.bold = True
run.font.name = 'Times New Roman'
run._element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
else:
para.paragraph_format.first_line_indent = Pt(32)
for run in para.runs:
run.font.size = Pt(16)
run.font.name = 'Times New Roman'
run._element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
lines += 1
file.save(path[0:-5]+'_new.docx')
if(pdf):
word = gencache.EnsureDispatch('Word.Application')
doc = word.Documents.Open(path[0:-5]+'_new.docx', ReadOnly=1)
doc.ExportAsFixedFormat(path[0:-5], constants.wdExportFormatPDF)
return True