提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
`随着人工智能的不断发展,Python开发语言也越来越普遍,针对文档的操作更是便捷,在办公时使用编写文档工具可大大简化工作难度,提升效率。
一、文档页数,字数,文档数统计工具
本文主要针对大批量文档的一个统计工作。
二、使用步骤
1.引入库
代码如下(示例):
import re
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from spire.doc import *
from spire.doc import Document
其中前端框架采用tkinter,文档操作使用的spire.doc第三方库,功能丰富,操作文档,基本已经内置了相应接口,直接拿来即用。
2.代码逻辑
代码如下(示例):
window = Tk() # 创建窗口对象
window.title("代码统计工具") # 设置窗口标题
window.geometry("1280x720") # 设置窗口大小
# 第5步,创建一个主frame,长在主window窗口上
frameT = tk.Frame(window).grid(row=0,column=0,columnspan=40)
chooseQueryType = 0
abnormalComList = ['.java','.vue']
# 用于获取文件夹下,Java文件中包含特定字符的内容
def getWordContent(folderPath):
paragraphCount = 0
charCount = 0
charCountWithspaces = 0
wordCount = 0
doc = Document()
pageCount = 0
wordCountNum = 0
for root, dirs, files in os.walk(folderPath):
for file in files:
if file.endswith(('.doc','.docx')):
# 载入lord文档
wordCountNum = wordCountNum +1
doc.LoadFromFile(os.path.join(root,file))
#更新字数统计(确保字数统计是最新的)doc.Updatewordcount()#获取文档内置属性
builtinProperties = doc.BuiltinDocumentProperties
# 获取页数
ttt = builtinProperties.PageCount
if ttt >= 0:
pageCount = pageCount + builtinProperties.PageCount
# 获取段落数
paragraphCount = paragraphCount + builtinProperties.ParagraphCount
# 获取字符数
charCount = charCount + builtinProperties.CharCount
# 获取包含空格的字符数
charCountWithspaces = charCountWithspaces + builtinProperties.CharCountWithSpace
# 获取字数
ttt = builtinProperties.WordCount
if ttt >= 0:
wordCount = wordCount + builtinProperties.WordCount
tk.Label(frameT, text="页数:"+str(pageCount), width=100).grid(row=2, column=0, columnspan=2)
# tk.Label(frameT, text="段落数:"+str(paragraphCount), width=100).grid(row=3, column=0, columnspan=2)
tk.Label(frameT, text="字符数:"+str(charCount), width=100).grid(row=4, column=0, columnspan=2)
tk.Label(frameT, text="包含空格的字符数:"+str(charCountWithspaces), width=100).grid(row=5, column=0, columnspan=2)
# tk.Label(frameT, text="字数:"+str(wordCount), width=100).grid(row=6, column=0, columnspan=2)
tk.Label(frameT, text="文件数:"+str(wordCountNum), width=100).grid(row=7, column=0, columnspan=2)
def tempCreate():
folderPath = filedialog.askdirectory()
tk.Label(frameT, text=folderPath, width=100).grid(row=1, column=1, columnspan=2)
print(folderPath)
getWordContent(folderPath)
# 第一行
b = tk.Button(frameT, text='文档统计选择文件夹', font=('Arial', 12), width=20, height=1, command=tempCreate)
b.grid(row=0,column=0)
tk.Label(frameT, text="文件夹路径:").grid(row=1,column=0)
# 用于获取文件夹下,Java文件中包含特定字符的内容
def getSpecificContent(folderPath, specificStr, specificStr2):
global chooseQueryType
rs = []
lineNum = 0
port_num = 0
for root, dirs, files in os.walk(folderPath):
for file in files:
if file.endswith(abnormalComList[chooseQueryType]):
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
lineNum = lineNum + len(content.splitlines())
print(lineNum)
if re.search(specificStr, content):
"""
lines = content.splitlines()
for line in lines:
# print(line.find(specificStr))
if line.find(specificStr) != -1 and line.find('http') == -1:
con = extract_quote_data(line)
for ser in con:
rs.append(ser)
"""
port_num = port_num+content.count(specificStr2)
# print(con)
# print("File: " + file_path)
# print("Content:")
# print(re.findall(specificStr, content))
print("-------------->所有代码行数:"+str(lineNum))
print("-------------->所有代码接口数:" + str(port_num))
tk.Label(frameT, text="-------------->所有代码行数:"+str(lineNum), width=100).grid(row=12, column=0, columnspan=2)
tk.Label(frameT, text="-------------->所有代码接口数:" + str(port_num), width=100).grid(row=13, column=0, columnspan=2)
return rs
def tempCreateCode():
kw = "@FeignClient"
kw1 = '@RestController'
kw2 = '@ApiOperation'
folderPath = filedialog.askdirectory()
tk.Label(frameT, text=folderPath, width=100).grid(row=11, column=1, columnspan=2)
print(folderPath)
# folderPath = 'D://project'
con = getSpecificContent(folderPath, kw1, kw2)
unique_set = set(tuple(con))
print(unique_set)
# 第二行
b = tk.Button(frameT, text='代码统计选择文件夹', font=('Arial', 12), width=20, height=1, command=tempCreateCode)
b.grid(row=10,column=0)
def abnorSele (event):
global chooseQueryType
chooseQueryType = abnorBox.current()
abnorBox = ttk.Combobox(frameT,values=abnormalComList,width=20,state="readonly")
abnorBox.set(abnormalComList[0]) # 设置默认选项
abnorBox.grid(row=10,column=1,columnspan=2)
abnorBox.bind("<<ComboboxSelected>>", abnorSele)
tk.Label(frameT, text="文件夹路径:").grid(row=11,column=0)
window.mainloop() # 运行窗口主循环
开发工具为pycharm,代码直接运行即可使用,前提是先导入对应第三方库。spire.doc使用的是12.4.0版本