P001-将指定目录下的java文件写入word文档中

JavaToWord.py


import os
import re
from docx import Document

# 定义函数,读取指定路径下的Java文件并返回其内容列表
def remove_comments(text):
    # 正则表达式匹配单行注释(#开头)或多行注释('''或"""包裹起来的部分)
    # pattern = r'(?:\/\*\*)|(?:\*\/)'
    # # pattern = r'((?:\*.\*$))'
    # # 使用sub()函数将匹配到的注释内容替换为空字符串
    # result = re.sub(pattern, '', text)

    # pattern = r'(?:\/\*\*)|(?:\*\/)'
    # # pattern = r'((?:\*.\*$))'
    # # 使用sub()函数将匹配到的注释内容替换为空字符串
    # result = re.sub(pattern, '', text)

    # /* 类型注释
    pattern1 = r'\/\*\*.*'
    result = re.sub(pattern1, '', text)

    # * 类型注释(此处会错误删除带* 的正常代码,需要注意)
    pattern2 = r'\*.*'
    result2 = re.sub(pattern2, '', result)

    # // 类型注释
    pattern3 = r'\/\/.*'
    result4 = re.sub(pattern3, '', result2)
    return result4
def read_java_files(path):
    java_files = []

    for root, dirs, files in os.walk(path):
        for file in files:
            if file.endswith(".java"):
                with open(os.path.join(root, file), "r", encoding="utf-8") as f:
                    content = f.read()
                    # 在这里进行对content的操作,如删除注释等
                    # ...
                    content = remove_comments(content)
                    java_files.append((file, content))
    return java_files


# 调用函数,传入当前目录路径
current_dir = os.getcwd()
java_files = read_java_files(current_dir)

# 创建新的Word文档
doc = Document()

for file, content in java_files:
    # 添加标题到Word文档
    doc.add_heading("{}".format(file), level=1)

    # 将处理过的内容按段落添加到Word文档
    paragraphs = [p for p in re.split("\n\n+", content)]
    for para in paragraphs:
        doc.add_paragraph(para)

# 保存Word文档
doc.save("output0951.docx")

如果没有doc库则安装 pip install python-docx
日期:2024-02-28

posted @ 2024-02-28 11:32  ysloong  阅读(28)  评论(0编辑  收藏  举报