场景:
需要把多个.cpp和.h文件转到一个word文档里面:
1、将.cpp和.h转为.txt文本文件
首先在该目录下新建文本文件.txt
,写入ren *.cpp *.txt
和ren *.h *.txt
,将该文件后缀名改为.bat,点击运行即可。由于冲突文件名可能会失败,需要手动改正为txt后缀。
2、批量txt转一个word
python代码:
from urllib3 import *
from re import *
import re
import os
import hashlib
import requests
from bs4 import BeautifulSoup
import bs4
for filename in os.listdir("D:\\res"): #原txt文件夹
with open("D:\\res\\"+filename) as f: #读取每个文件
flag=0
for line in f.readlines(): #将每个文件文本统一逐行写入一个word中
with open("word版/全部文书整理版.doc","a") as mom:
if flag==0:
mom.write(filename) #每行开头写入文件名
flag=1
mom.write('\n'+line)