You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import os
# 工作目录
working_dir = r"D:/dsWork/YunNanDsBase/Doc/全省及州市县区人口与教育报告集20241023/16个州市报告2022/分析报告20240510/"
# 在工作目录下创建Excel目录
excel_dir = working_dir + 'Excel'
if not os.path.exists(excel_dir):
os.mkdir(excel_dir)
# 关键词
keyword = '人口变化及其对教育的影响'
# 遍历工作目录下所有的docx文件将文件名用keyword进行分隔前一半是州市名称后一半是上报的时间我们取前一半的州市名称
for file in os.listdir(working_dir):
if file.endswith('.docx'):
file_name = file.split('.')[0]
# 判断一下file_name中是不是存在keyword,如果不存在,则输出错误,并结束程序
if keyword not in file_name:
print('Error: ' + file_name + ' 文件名称中并不包含:' + keyword)
exit()
# 确认包含后,提取出前半部分作为城市名称
city_name = file_name.split(keyword)[0]
# 在excel_dir目录下创建这个城市的子目录准备将生成的excel文件放在这个子目录下
city_dir = excel_dir + '/' + city_name
if not os.path.exists(city_dir):
os.mkdir(city_dir)