This article mainly describes the python read the specified directory under the specified suffix file and save as docx, the need for friends can refer to the following
Recently, there's a wonderful flower that asks for a patent for the n lines of code in the project.
Then as programmers of course cannot copy paste with code to solve:
Using Python-docx to read and write docx files
Environment Use python3.6.0
First PIP installation Python-docx
Pip Install Python-docx
Then here is the script to modify the directory, here default to the script run directory under the SRC folder
All files that take the. cs suffix are read and saved as docx
One thing to note, if there is Chinese in the file, please use the Vscode or other editor to open the Utf-8 format, to see if there is no garbled each file will have the print output when you see only---Start no end can find the file to see if there is the above mentioned situation , save the re-execution after the modification, until all the execution is complete, save the docx file.
Code
#--Coding:utf-8--# Created by luody on 2017/4/7.import osfrom docx import documentsavefile = OS . GETCWD () + "/code.docx" MyPath = OS.GETCWD () + "/src" doc = Document () doc.add_heading ("code document", 0) p = doc.add_paragraph (' Server Code, using the language ') p.add_run (' C#,sql '). Bold = Truelinenum = 0for root, dirs, files in Os.walk (mypath): for Filespath in Files:i F (Filespath.endswith ('. CS ')): Doc.add_heading (Filespath, level=1) codePage = ' Print (filespath+ '----STA RT ') for line in open (Os.path.join (root, Filespath), encoding= "Utf-8"): CodePage + = line LineNum + = 1 Print (filespath+ '----End ') doc.add_paragraph (codePage, style= ' Intensequote ') doc.add_page_break () p = doc.a Dd_paragraph (U ' total number of rows: ') P.add_run (str (linenum)). Bold = Truedoc.save (' Code.docx ') print (linenum)