自动转换公文格式

本工具基于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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值