|
|
|
@ -2,9 +2,7 @@ import os
|
|
|
|
|
import win32com
|
|
|
|
|
from win32com.client import Dispatch
|
|
|
|
|
|
|
|
|
|
# pip uninstall pywin32
|
|
|
|
|
# pip install pywin32
|
|
|
|
|
# 工作目录
|
|
|
|
|
working_dir = r"D:/dsWork/YunNanDsBase/Doc/全省及州市县区人口与教育报告集20241023/16个州市报告2022/分析报告20240510/"
|
|
|
|
|
|
|
|
|
|
# 在工作目录下创建Excel目录
|
|
|
|
@ -15,20 +13,22 @@ if not os.path.exists(excel_dir):
|
|
|
|
|
# 关键词
|
|
|
|
|
keyword = '人口变化及其对教育的影响'
|
|
|
|
|
|
|
|
|
|
# 当输出:pywintypes.error 错误时,检查办法
|
|
|
|
|
# https://www.cnblogs.com/rong-z/p/18245281
|
|
|
|
|
import win32api
|
|
|
|
|
|
|
|
|
|
# print(win32api.FormatMessage(-2146822496))
|
|
|
|
|
|
|
|
|
|
# 是不是打Word显示
|
|
|
|
|
docApp = win32com.client.Dispatch('Word.Application')
|
|
|
|
|
# 是不是打Word显示
|
|
|
|
|
docApp.Visible = False
|
|
|
|
|
docApp.DisplayAlerts = 0
|
|
|
|
|
|
|
|
|
|
# doc = docApp.Documents.Open('c:/1.docx')
|
|
|
|
|
# doc = docApp.Documents.Open('c:/昭通市人口变化及其对教育的影响20240416.docx')
|
|
|
|
|
# doc = docApp.Documents.Open('c:/昆明市人口变化及其对教育的影响20240419.docx')
|
|
|
|
|
# doc = docApp.Documents.Open(working_dir+'红河哈尼族彝族自治州人口变化及其对教育的影响20240419.docx')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 遍历工作目录下所有的docx文件,将文件名用keyword进行分隔,前一半是州市名称,后一半是上报的时间,我们取前一半的州市名称
|
|
|
|
|
for file in os.listdir(working_dir):
|
|
|
|
|
if file.endswith('.docx'):
|
|
|
|
|
if file.endswith('.docx') and not file.startswith('~'):
|
|
|
|
|
file_name = file.split('.')[0]
|
|
|
|
|
# 判断一下file_name中是不是存在keyword,如果不存在,则输出错误,并结束程序
|
|
|
|
|
if keyword not in file_name:
|
|
|
|
@ -42,6 +42,7 @@ for file in os.listdir(working_dir):
|
|
|
|
|
os.mkdir(city_dir)
|
|
|
|
|
# 将当前docx进行读取其中的每一个段落,要求以 "图"+数字开头,这是图例的意思
|
|
|
|
|
doc_path = working_dir + '/' + file
|
|
|
|
|
# print(doc_path)
|
|
|
|
|
doc = docApp.Documents.Open(doc_path)
|
|
|
|
|
# 遍历文档中所有的文字段落,判断是不是以 图+数字开头
|
|
|
|
|
idx = 1
|
|
|
|
@ -71,7 +72,6 @@ for file in os.listdir(working_dir):
|
|
|
|
|
# 下一个图表的索引号
|
|
|
|
|
idx = idx + 1
|
|
|
|
|
print(idx - 1)
|
|
|
|
|
|
|
|
|
|
# 关闭文档和Word应用
|
|
|
|
|
doc.Close()
|
|
|
|
|
docApp.Quit()
|
|
|
|
|