Python reads the specified suffix file in the specified directory and saves it as docx

  • 2020-05-30 20:27:42
  • OfStack

Recently there was a bizarre request to patent the N line of code in the project

Then as a programmer of course can not copy and paste with code to solve.

Read and write docx files using python-docx

The environment USES python3.6.0

First, pip installs python-docx

pip install python-docx

Next is the script modification directory, where the src folder under the script run directory is taken by default
Take all files with the.cs suffix to read and save as docx

One point to note, if the file is in Chinese, please use vscode or other editors use utf - eight open format, see if there are any garbled each processing a file will be print output when you see only - no end start can find the file to check whether there is a saying, modified save rerun, 1 until all completed, save good docx file


# -- coding: UTF-8 --
# Created by luody on 2017/4/7.
import os
from docx import Document
saveFile = os.getcwd() + "/code.docx"
mypath = os.getcwd() + "/src"
doc = Document()
doc.add_heading(" Code documentation ", 0)
p = doc.add_paragraph(' Server code , Use the language ')
p.add_run('C#,SQL').bold = True
lineNum = 0
for root, dirs, files in os.walk(mypath):
  for filespath in files:
    if (filespath.endswith('.cs')):
      doc.add_heading(filespath, level=1)
      codePage = ''
      print(filespath+' ---- start')
      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')
p = doc.add_paragraph(u' Total number of rows :')
p.add_run(str(lineNum)).bold = True'code.docx')

Related articles: