软著申请源代码整合脚本

前言

最近想要申请软著,发现需要提交60页的源代码,还有格式上的要求,所以写了个脚本来自动将代码整合到word文档中,自动去除了注释和空行,并会统计总行数。

此脚本需要安装Python环境,并安装python-docx模块。
此脚本会整合所有代码文件,并生成一个word文档,但不能自动删减页数到60页,如果代码过多,需要手动删减。

脚本

使用前需要用pip install python-docx安装python-docx模块。

将文件放入代码根目录,使用python .\word.py运行脚本,会在当前目录生成一个code.docx文件,打开后就可以看到整合后的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import re
# pip install python-docx
from docx import Document
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_LINE_SPACING, WD_ALIGN_PARAGRAPH

exclude_dirs = ['.git', '__pycache__', 'backup', 'logs', '.idea', 'allatori', 'target'] # 排除的文件夹/文件
suffix_list = ['.py'] # 要处理的文件后缀


# 获取指定目录下的所有文件
def get_java_files(directory):
java_files = []
for root, dirs, files in os.walk(directory):
# 排除指定文件夹
if any(exclude_dir in root for exclude_dir in exclude_dirs):
continue
for file in files:
# 排除本文件
if os.path.basename(file) == os.path.basename(__file__):
continue
# 只处理指定后缀
if any(file.endswith(suffix) for suffix in suffix_list):
java_files.append(os.path.join(root, file).replace('\\', '/'))
return java_files


# 返回删掉空行和注释的内容
def read_java_file(file_path):
with open(file_path, 'r', encoding='utf-8') as ff:
file_content = ff.read()
file_content = re.sub('![^\$][^\n]*', '', file_content)
# 按\n分割成列表
file_lines = file_content.splitlines()
for i in file_lines[:]:
if i == '':
file_lines.remove(i)
if re.match(r'^\s+$', i):
file_lines.remove(i)
res = '\n'.join(file_lines) + '\n'
return res, len(file_lines)


# 将处理后的代码写入并保存到指定doc文件下
def saveDocFile(file_list, save_path, base_path=os.getcwd()):
'''
:param file_list: 要处理的文件列表
:param save_path: 保存路径
:param base_path: 相对路径的基准路径,默认当前路径
'''
# WD_LINE_SPACING
# SINGLE => 单倍行距(默认)
# ONE_POINT_FIVE => 1.5倍行距
# DOUBLE2 => 倍行距
# AT_LEAST => 最小值
# EXACTLY => 固定值
# MULTIPLE => 多倍行距
doc = Document()
# doc.add_heading('项目源代码整合', 0) # 标题

# 设置正文格式
doc.styles['Normal'].font.name = 'Times New Roman' # 正文是normal, 设置正文的字体格式
doc.styles['Normal'].font.size = Pt(8) # 设置字体的大小为 5 号字体
paragraph_format = doc.styles['Normal'].paragraph_format
paragraph_format.line_spacing = Pt(12.9) # 固定值12,9磅, 保证每页有50行代码
lines = 0

# 添加代码和标题
for i, f in enumerate(file_list):
tempStr = '正在处理文件 %d/%d' % (i + 1, len(file_list))
relative_path = os.path.relpath(f, base_path).replace('/', '\\') # 相对路径
print(tempStr + " -> " + relative_path)

# 添加文件路径为标题(如不需要,注释掉此段落)
p = doc.add_paragraph()
title_run = p.add_run(relative_path + '\n')
title_run.font.size = Pt(10)
title_run.font.color.rgb = RGBColor(0, 0, 0)
title_run.bold = True

# 读取代码文件
code_content, code_lines = read_java_file(f)
lines += code_lines

# 添加代码内容
code_run = p.add_run(code_content) # 代码接在标题后
code_run.font.size = Pt(8) # 确保代码字体小于标题
p.line_spacing_rule = WD_LINE_SPACING.EXACTLY # 固定行距

print("项目源代码已经整合到 Word 文档中。")
print(f"代码总行数: {lines}")
doc.save(save_path)


if __name__ == '__main__':
path = os.getcwd()
savePath = path + os.sep + "code.docx"
saveDocFile(get_java_files(path), savePath, base_path=path)