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.

85 lines
3.8 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
import win32com
from win32com.client import Dispatch
import re
# pip install pywin32 openpyxl
# pip install pywin32
working_dir = r"D:/dsWork/YunNanDsBase/Doc/全省及州市县区人口与教育报告集20241023/16个州市报告2022/分析报告20240510/"
import openpyxl
# 在工作目录下创建Excel目录
excel_dir = working_dir + 'Excel'
if not os.path.exists(excel_dir):
os.mkdir(excel_dir)
# 关键词
keyword = '人口变化及其对教育的影响'
# 是不是打Word显示
docApp = win32com.client.Dispatch('Word.Application')
# 是不是打Word显示
docApp.Visible = False
docApp.DisplayAlerts = 0
# 遍历工作目录下所有的docx文件将文件名用keyword进行分隔前一半是州市名称后一半是上报的时间我们取前一半的州市名称
for file in os.listdir(working_dir):
if file.endswith('.docx') and not file.startswith('~'):
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)
# 将当前docx进行读取其中的每一个段落要求以 "图"+数字开头,这是图例的意思
doc_path = working_dir + '/' + file
# print(doc_path)
doc = docApp.Documents.Open(doc_path)
# 遍历文档中所有的文字段落,判断是不是以 图+数字开头
idx = 1
# 图表的名称列表
tb_list = []
for para in doc.Paragraphs:
x = para.Range.Text.strip().replace("", "").replace(" ", " ")
if x.startswith(""):
tb_list.append(x)
idx = idx + 1
# 遍历文档中的所有内嵌形状
idx = 1
for inline_shape in doc.InlineShapes:
if inline_shape.Type == win32com.client.constants.wdInlineShapeChart: # 检查是否为内嵌图表
shape = doc.InlineShapes(idx)
sheet = shape.Chart.ChartData.Workbook.Worksheets(1)
# 创建一个新的Excel工作簿
wb = openpyxl.Workbook()
ws = wb.active
# 遍历Excel工作表中的所有单元格并将其写入新的工作簿
for row in range(1, sheet.UsedRange.Rows.Count + 1):
for col in range(1, sheet.UsedRange.Columns.Count + 1):
cell_value = sheet.Cells(row, col).Value
ws.cell(row=row, column=col, value=cell_value)
# 保存新的Excel文件
original_string = tb_list[idx - 1]
# 使用正则表达式过滤,只保留中文、英文和数字
original_string = original_string[1:]
if ' ' in original_string:
original_string = original_string.split(" ")[1]
filtered_string = re.sub(r'[^\u4e00-\u9fa5a-zA-Z0-9]', '', original_string)
fileName = '' + str(idx) + '' + filtered_string + ".xlsx"
wb.save(city_dir + '/' + fileName)
print("保存文件:" + fileName)
# 下一个图表的索引号
idx = idx + 1
# print(idx - 1)
# 关闭文档和Word应用
doc.Close()
docApp.Quit()
print("恭喜,所有市州数据整理工作成功完成!")