当前位置:   article > 正文

用Python编写的Word一键排版工具_python实现word 一键排版工具

python实现word 一键排版工具

       在我们每天的工作中,可能需要不断地撰写一些计划、方案、总结,而这些方案很可能大多数是从网络上下载下来的。现在网络上能免费下载的资源非常少,我们采用的主要下载方式是通过截屏识别文字来实现文本复制的。复制下来的文本粘贴到word文档当中,还需要我们进一步排版。有些单位对文本格式的要求是特别严格的,必须按照政府部门规定的相关格式进行排版。

例如,以下要求为某单位的排版格式。

文字性材料排版格式要求

标题:方正小标宋-GBK二号,正文:仿宋GB2312三号,一级标题黑体,二级标题楷体,三级标题仿宋加粗。页面设置,上下边距3.7cm、3.5cm,左右边距2.8cm、2.6cm。行距磅值:固定值28磅。文章最后空4行署名、时间,具体位置在靠右边4汉字符的位置,时间用小写阿拉伯数字如2023年7月25日。

       在这种情形下,我们撰写、下载并修改文本需要一定的时间,文章修改完之后还要按照格式进行排版,这也需要花费大量的时间。有些年龄大的职工仅完成计划、方案及总结的撰写这几项工作已累得疲惫不堪,而对于排版,真是一窍不通。

       今天就给大家分享一款自己编写的Word文档一键排版软件,可以轻松帮助我们完成Word文档公文排版,即使你在电脑方面是小白,也可以一键搞定。从此,公文排版,再也不用求人啦,办公的文秘人员也可以腾出更多时间和精力去从事其他工作。

 排版前

 

 排版后

 排版后

       程序已完成了对文档标题、作者单位及一、二、三、四级标题的智能识别,运行效果基本满意。现将代码作以分享。

  1. import docx
  2. from docx.oxml.ns import qn
  3. from docx.shared import Pt, Cm, Mm
  4. from docx.enum.text import *
  5. import os
  6. import sys
  7. from docx import Document
  8. from PyQt5.QtWidgets import QApplication, QFileDialog
  9. # 删除段落
  10. def delete_paragraph(paragraph):
  11. p = paragraph._element
  12. p.getparent().remove(p)
  13. # p._p = p._element = None
  14. paragraph._p = paragraph._element = None
  15. #判断是否为落款格式
  16. def LuoKuan(str):
  17. for i in str:
  18. if i in punc:
  19. return False
  20. if ((str[0] in num) and (str[-1] == "日") and (len(str) <= 12)) or ((str[0] in cn_num) and (str[-1] == "日") and (len(str) <= 12)):
  21. return True
  22. else:
  23. return False
  24. def setMargin(docx):
  25. section = docx.sections[0]
  26. section.page_height = Cm(29.7)
  27. section.page_width = Cm(21.0)
  28. section.left_margin = Cm(2.8)
  29. section.right_margin = Cm(2.6)
  30. section.top_margin = Cm(3.7)
  31. section.bottom_margin = Cm(3.5)
  32. #判断是否为一级标题格式(如:一、xxx)
  33. def GradeOneTitle(str):
  34. if ((str[0] in cn_num) and (str[1] == "、")) or ((str[0] in cn_num) and (str[1] in cn_num) and (str[2] == "、")):
  35. return True
  36. else:
  37. return False
  38. #判断是否为二级标题格式(如:(一)xxx)
  39. def GradeTwoTitle(str):
  40. if ((str[0] == "(") and (str[1] in cn_num) and (str[2] == ")")) or ((str[0] == "(") and (str[1] in cn_num) and (str[2] in cn_num) and (str[3] == ")")):
  41. return True
  42. else:
  43. return False
  44. #判断是否为三级标题格式(如:1.xxx)
  45. def GradeThreeTitle(str):
  46. if ((str[0] in num) and (str[1] in punc)) or ((str[0] in num) and (str[1] in num) and (str[2] in punc)):
  47. return True
  48. else:
  49. return False
  50. #判断是否为四级标题格式(如:(1)xxx)
  51. def GradeFourTitle(str):
  52. if ((str[0] == "(") and (str[1] in num) and (str[2] == ")")) or ((str[0] == "(") and (str[1] in num) and (str[2] in num) and (str[3] == ")")):
  53. return True
  54. else:
  55. return False
  56. #判断是否为五级标题格式(如:一是XXX)
  57. def GradeFiveTitle(str):
  58. if ((str[0] in cn_num) and (str[1] in must)) or ((str[0] in cn_num) and (str[1] in cn_num) and (str[1] in must)):
  59. return True
  60. else:
  61. return False
  62. def OneKeyWord():
  63. global cn_num,num,punc,must
  64. cn_num = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
  65. num = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
  66. punc = ["。", ",", "!", "?", ":", ";", "、", ".", "(", ")","."]
  67. must = ["要", "是", "能"]
  68. filecnt = 0
  69. print('欢迎使用Word一键排版工具!创作者QQ:124500535')
  70. confirm= input("是否打开Word文档?输入“Y”表示“打开”,输入“N”表示“取消”!")
  71. if confirm == 'Y' or confirm == 'y':
  72. a = QApplication([''])
  73. files, stylel = QFileDialog.getOpenFileNames(caption="多文件选择", directory="/", filter="Word 文档(*.docx)")
  74. print(files) # 打印所选文件全部路径(包括文件名和后缀名)和文件类型
  75. for file in files:
  76. docx = Document(file)
  77. paragraphcnt = 0
  78. filecnt= filecnt+1
  79. print('这是第%s个文件:%s' %(filecnt,file))
  80. for paragraph in docx.paragraphs:
  81. paragraphcnt = paragraphcnt +1
  82. paragraph.text=paragraph.text.replace(",",",")
  83. paragraph.text=paragraph.text.replace(";",";")
  84. paragraph.text=paragraph.text.replace(":",":")
  85. paragraph.text=paragraph.text.replace("!","!")
  86. paragraph.text=paragraph.text.replace("?","?")
  87. paragraph.text=paragraph.text.replace("(","(")
  88. paragraph.text=paragraph.text.replace(")",")")
  89. paragraph.text=paragraph.text.replace(" ","")
  90. paragraph.text=paragraph.text.replace("\t", "")
  91. paragraph.text = paragraph.text.replace("\n", "")
  92. if paragraph.text == '':
  93. delete_paragraph(paragraph)
  94. paragraphcnt = paragraphcnt-1
  95. continue
  96. paragraph.paragraph_format.left_indent = 0 #预先对缩进赋值, 防止对象为空报错
  97. paragraph.paragraph_format.element.pPr.ind.set(qn("w:firstLineChars"), '0') #并去除缩进
  98. paragraph.paragraph_format.element.pPr.ind.set(qn("w:firstLine"), '0')
  99. paragraph.paragraph_format.element.pPr.ind.set(qn("w:leftChars"), '0')
  100. paragraph.paragraph_format.element.pPr.ind.set(qn("w:left"), '0')
  101. paragraph.paragraph_format.element.pPr.ind.set(qn("w:rightChars"), '0')
  102. paragraph.paragraph_format.element.pPr.ind.set(qn("w:right"), '0')
  103. print('这是第%s段' %paragraphcnt)
  104. print(paragraph.text)
  105. if paragraphcnt == 1 and len(paragraph.text)<40:
  106. #处理头部空行
  107. #标题(方正小标宋_GBK、2号、加粗、居中、下端按2号字空一行)
  108. paragraph.paragraph_format.line_spacing=Pt(28) #行距固定值28磅
  109. paragraph.paragraph_format.space_after = Pt(0) #段后间距=0
  110. for run in paragraph.runs:
  111. run.font.size = Pt(22) # 字体大小2号
  112. run.bold = False # 加粗
  113. run.font.name = '方正小标宋_GBK' # 控制是西文时的字体
  114. run.element.rPr.rFonts.set(qn('w:eastAsia'), '方正小标宋_GBK') # 控制是中文时的字体
  115. paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER # 居中
  116. continue
  117. elif paragraphcnt == 2 and len(paragraph.text) < 30:
  118. # 作者单位、姓名
  119. paragraph.paragraph_format.line_spacing = Pt(28) # 行距固定值28磅
  120. paragraph.paragraph_format.space_after = Pt(0) # 段后间距=0
  121. for run in paragraph.runs:
  122. run.font.size = Pt(16) # 字体大小2号
  123. run.bold = False # 加粗
  124. run.font.name = '楷体' # 控制是西文时的字体
  125. run.element.rPr.rFonts.set(qn('w:eastAsia'), '楷体') # 控制是中文时的字体
  126. paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER # 居中
  127. continue
  128. elif paragraphcnt == 3 and len(paragraph.text) < 30 and (paragraph.text[0] == "(") and (paragraph.text[1] in num):
  129. # 日期,如(2023年6月15日)
  130. paragraph.paragraph_format.line_spacing = Pt(28) # 行距固定值28磅
  131. paragraph.paragraph_format.space_after = Pt(0) # 段后间距=0
  132. for run in paragraph.runs:
  133. run.font.size = Pt(16) # 字体大小2号
  134. run.bold = False # 加粗
  135. run.font.name = '楷体' # 控制是西文时的字体
  136. run.element.rPr.rFonts.set(qn('w:eastAsia'), '楷体') # 控制是中文时的字体
  137. paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER # 居中
  138. continue
  139. # #处理正文
  140. else:
  141. paragraph.paragraph_format.line_spacing = Pt(28) # 行距固定值28磅
  142. paragraph.paragraph_format.space_after = Pt(0) # 段后间距=0
  143. paragraph.paragraph_format.first_line_indent = Pt(32)
  144. for run in paragraph.runs:
  145. run.font.size = Pt(16) # 字体大小3号
  146. run.bold = False # 字体不加粗
  147. run.font.name = '仿宋_GB2312'
  148. run.element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  149. if GradeOneTitle(run.text): #判断是否为一级标题格式(如:一、xxx)
  150. run.font.name = '黑体'
  151. run.element.rPr.rFonts.set(qn('w:eastAsia'), '黑体')
  152. elif GradeTwoTitle(run.text): #判断是否为二级标题格式(如:(一)xxx)
  153. if "。" not in run.text:
  154. run.font.name = '楷体'
  155. run.element.rPr.rFonts.set(qn('w:eastAsia'), '楷体')
  156. else:
  157. run.text = run.text.split('。',1)
  158. run.font.name = '楷体'
  159. run.element.rPr.rFonts.set(qn('w:eastAsia'), '楷体')
  160. elif GradeThreeTitle(run.text): #判断是否为三级标题格式(如:1.xxx)
  161. if "。" not in run.text:
  162. if (run.text[0] in num) and (run.text[1] in punc):
  163. run.text = run.text.replace(run.text[1], ".",1)
  164. if (run.text[0] in num) and (run.text[1] in num) and (run.text[2] in punc):
  165. run.text = run.text.replace(run.text[2], ".", 1)
  166. run.font.name = '仿宋_GB2312'
  167. run.element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  168. run.bold = True # 字体加粗
  169. else:
  170. if (run.text[0] in num) and (run.text[1] in punc):
  171. run.text = run.text.replace(run.text[1], ".", 1)
  172. if (run.text[0] in num) and (run.text[1] in num) and (run.text[2] in punc):
  173. run.text = run.text.replace(run.text[2], ".", 1)
  174. sentence_to_bold = run.text.split('。')[0]+"。"
  175. sentence_not_to_bold = run.text.split('。',1)[1]
  176. paragraph.insert_paragraph_before(sentence_to_bold)
  177. docx.paragraphs[paragraphcnt - 1].paragraph_format.first_line_indent = Pt(32)
  178. docx.paragraphs[paragraphcnt - 1].paragraph_format.line_spacing = Pt(28) # 行距固定值28磅
  179. docx.paragraphs[paragraphcnt - 1].paragraph_format.space_after = Pt(0) # 段后间距=0
  180. docx.paragraphs[paragraphcnt - 1].runs[0].font.name = '仿宋_GB2312'
  181. docx.paragraphs[paragraphcnt - 1].runs[0].font.size = Pt(16) # 字体大小3号
  182. docx.paragraphs[paragraphcnt - 1].runs[0].element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  183. docx.paragraphs[paragraphcnt - 1].runs[0].bold = True # 字体加粗
  184. docx.paragraphs[paragraphcnt - 1].add_run(sentence_not_to_bold).bold = False
  185. docx.paragraphs[paragraphcnt - 1].runs[1].font.name = '仿宋_GB2312'
  186. docx.paragraphs[paragraphcnt - 1].runs[1].font.size = Pt(16) # 字体大小3号
  187. docx.paragraphs[paragraphcnt - 1].runs[1].element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  188. delete_paragraph(paragraph)
  189. elif GradeFourTitle(run.text): #判断是否为四级标题格式(如:(1)xxx)
  190. if "。" not in run.text:
  191. run.font.name = '仿宋_GB2312'
  192. run.element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  193. run.bold = True # 字体加粗
  194. else:
  195. sentence_to_bold = run.text.split('。')[0]+"。"
  196. sentence_not_to_bold = run.text.split('。',1)[1]
  197. paragraph.insert_paragraph_before(sentence_to_bold)
  198. docx.paragraphs[paragraphcnt - 1].paragraph_format.first_line_indent = Pt(32)
  199. docx.paragraphs[paragraphcnt - 1].paragraph_format.line_spacing = Pt(28) # 行距固定值28磅
  200. docx.paragraphs[paragraphcnt - 1].paragraph_format.space_after = Pt(0) # 段后间距=0
  201. docx.paragraphs[paragraphcnt - 1].runs[0].font.name = '仿宋_GB2312'
  202. docx.paragraphs[paragraphcnt - 1].runs[0].font.size = Pt(16) # 字体大小3号
  203. docx.paragraphs[paragraphcnt - 1].runs[0].element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  204. docx.paragraphs[paragraphcnt - 1].runs[0].bold = True # 字体加粗
  205. docx.paragraphs[paragraphcnt - 1].add_run(sentence_not_to_bold).bold = False
  206. docx.paragraphs[paragraphcnt - 1].runs[1].font.name = '仿宋_GB2312'
  207. docx.paragraphs[paragraphcnt - 1].runs[1].font.size = Pt(16) # 字体大小3号
  208. docx.paragraphs[paragraphcnt - 1].runs[1].element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  209. delete_paragraph(paragraph)
  210. elif GradeFiveTitle(run.text): #判断是否为五级标题格式(如:一是xxx)
  211. if "。" not in run.text:
  212. run.font.name = '仿宋_GB2312'
  213. run.element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  214. run.bold = True # 字体加粗
  215. else:
  216. sentence_to_bold = run.text.split('。')[0]+"。"
  217. sentence_not_to_bold = run.text.split('。',1)[1]
  218. paragraph.insert_paragraph_before(sentence_to_bold)
  219. docx.paragraphs[paragraphcnt - 1].paragraph_format.first_line_indent = Pt(32)
  220. docx.paragraphs[paragraphcnt - 1].paragraph_format.line_spacing = Pt(28) # 行距固定值28磅
  221. docx.paragraphs[paragraphcnt - 1].paragraph_format.space_after = Pt(0) # 段后间距=0
  222. docx.paragraphs[paragraphcnt - 1].runs[0].font.name = '仿宋_GB2312'
  223. docx.paragraphs[paragraphcnt - 1].runs[0].font.size = Pt(16) # 字体大小3号
  224. docx.paragraphs[paragraphcnt - 1].runs[0].element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  225. docx.paragraphs[paragraphcnt - 1].runs[0].bold = True # 字体加粗
  226. docx.paragraphs[paragraphcnt - 1].add_run(sentence_not_to_bold).bold = False
  227. docx.paragraphs[paragraphcnt - 1].runs[1].font.name = '仿宋_GB2312'
  228. docx.paragraphs[paragraphcnt - 1].runs[1].font.size = Pt(16) # 字体大小3号
  229. docx.paragraphs[paragraphcnt - 1].runs[1].element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  230. delete_paragraph(paragraph)
  231. elif LuoKuan(run.text): # 判断是否为落款格式
  232. run.font.name = '仿宋_GB2312'
  233. run.element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  234. run.text = "\r" * 2 + run.text # 前置空格,顶到最右,需手动调整空格
  235. paragraph.paragraph_format.left_indent = Pt(288) #18B*16Pt=288Pt
  236. else: #普通正文格式
  237. run.font.name = '仿宋_GB2312'
  238. run.element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋_GB2312')
  239. setMargin(docx)
  240. docx.save(file)
  241. if __name__ == '__main__':
  242. OneKeyWord()
  243. os.system("pause")

        这款软件还有一个优点,就是可以批量处理word。但也有一些缺点,就是暂时只支持docx格式的Word文档。同时,在今天早上帮助同事运用些软件排版时,无意中发现,软件竟无法识别word文档中使用了自动编号的标题,再者界面过于丑陋,后期将进下一步完善代码,力争出现新的迭代UI界面作品,敬请关注!

        编程不易,且用且珍惜!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/1001676
推荐阅读
相关标签
  

闽ICP备14008679号