Describes how Python reads the specified suffix file in the specified directory and saves it as a docx method

Source: Internet
Author: User
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) 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.