main
黄海 9 months ago
parent b0cd35f4c0
commit 9d49eeb677

@ -8,6 +8,12 @@ import re
working_dir = r"D:\dsWork\YunNanDsBase\Doc\全省及州市县区人口与教育报告集20241023\133个县区报告2022\县区研究报告"
import openpyxl
# 是不是打Word显示
docApp = win32com.client.Dispatch('Word.Application')
# 是不是打Word显示
docApp.Visible = False
docApp.DisplayAlerts = 0
# 在工作目录下创建Excel目录
excel_dir = r'D:\dsWork\YunNanDsBase\Doc\全省及州市县区人口与教育报告集20241023\133个县区报告2022\Excel'
if not os.path.exists(excel_dir):
@ -25,7 +31,7 @@ for root, dirs, files in os.walk(working_dir):
file_path = os.path.join(county_dir, file)
# 判断文件是否是Word文档
if file_path.endswith('.docx') and not file.startswith('~'):
cityName=file_path.replace(working_dir,'')[1:].split("各县")[0]
cityName = file_path.replace(working_dir, '')[1:].split("各县")[0]
areaName = file
areaName = re.sub(r'[^\u4e00-\u9fa5]', '', areaName)
if '' not in areaName and '' not in areaName and '' not in areaName:
@ -44,7 +50,54 @@ for root, dirs, files in os.walk(working_dir):
line = line.strip()
# 将文本中的关键字替换为空字符串
areaName = areaName.replace(line.split(' ')[0], line.split(' ')[1])
#
print(f"正在处理县区:{cityName}{areaName}")
# 检查Excel目录下是不是存在这个城市的文件夹如果不存在则创建
city_dir = os.path.join(excel_dir, cityName)
if not os.path.exists(city_dir):
os.mkdir(city_dir)
# 使用word读取图表的技术保存EXCEL文件到城市的目录下
doc = docApp.Documents.Open(file_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文件
try:
original_string = tb_list[idx - 1]
except:
continue
# 使用正则表达式过滤,只保留中文、英文和数字
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
# 关闭文档和Word应用
doc.Close()
print(f"县区处理完成:{cityName}{areaName}")
docApp.Quit()
print("恭喜,所有县区数据整理工作成功完成!")

Loading…
Cancel
Save