代码文件导到word里

时间:2023-01-18 18:22:27
# 创建并写入word文档
import docx
import sys
import os

dir = "D:\\gitwork\\fm.qimeng.c-class\\src\\main\\java"
files = os.listdir(dir)
doc = docx.Document()


def lsDir(dir):
    files = os.listdir(dir)
    for f in files:
        if os.path.isdir(os.path.join(dir, f)):
            lsDir(os.path.join(dir, f))
        else:
            fpath = os.path.join(dir, f)
            writeWord(fpath)


def writeWord(fpath):
    print("处理" + fpath)
    with open(fpath, encoding='UTF-8') as f:
        lines = f.read()
        doc.add_paragraph(lines)


lsDir(dir)
doc.save("D:\\writeResult.docx")

 参考:https://www.cnblogs.com/rencm/p/6285304.html