mxdupdate.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # -*- coding: utf-8 -*-
  2. import arcpy
  3. import os
  4. import re
  5. def update_sde_connection_in_mxd(mxd_path, old_ip, new_ip):
  6. """
  7. 更新 MXD 文件中所有图层的 SDE 连接 IP
  8. 参数:
  9. mxd_path: MXD 文件路径
  10. old_ip: 旧的 IP 地址(可以是部分匹配)
  11. new_ip: 新的 IP 地址
  12. """
  13. try:
  14. # 打开 MXD
  15. mxd = arcpy.mapping.MapDocument(mxd_path)
  16. print("\n处理文件: {os.path.basename(mxd_path)}")
  17. # 更新数据框中的所有图层
  18. layers_updated = 0
  19. for df in arcpy.mapping.ListDataFrames(mxd):
  20. for lyr in arcpy.mapping.ListLayers(mxd, "", df):
  21. if lyr.supports("DATASOURCE"):
  22. # 获取当前数据源
  23. current_source = lyr.dataSource
  24. # 检查是否为 SDE 连接并包含旧 IP
  25. if "sde" in current_source.lower() and old_ip in current_source:
  26. # 构建新的数据源路径
  27. new_source = current_source.replace(old_ip, new_ip)
  28. try:
  29. # 更新图层数据源
  30. lyr.replaceDataSource(
  31. os.path.dirname(new_source), # 工作空间路径
  32. "SDE_WORKSPACE", # 工作空间类型
  33. os.path.basename(new_source), # 数据集名称
  34. False # 不验证
  35. )
  36. layers_updated += 1
  37. print(" 已更新: {lyr.name}")
  38. print(" 原连接: {current_source}")
  39. print(" 新连接: {new_source}")
  40. except Exception as e:
  41. print(" 更新失败 {lyr.name}: {str(e)}")
  42. # 保存 MXD
  43. if layers_updated > 0:
  44. mxd.save()
  45. print(" 保存成功,更新了 {layers_updated} 个图层")
  46. else:
  47. print(" 未找到需要更新的 SDE 连接")
  48. # 释放 MXD 对象
  49. del mxd
  50. return True
  51. except Exception as e:
  52. print("处理失败 {mxd_path}: {str(e)}")
  53. return False
  54. def batch_update_sde_connections(folder_path, old_ip, new_ip, recursive=False):
  55. """
  56. 批量处理文件夹中的 MXD 文件
  57. 参数:
  58. folder_path: 文件夹路径
  59. old_ip: 旧的 IP 地址
  60. new_ip: 新的 IP 地址
  61. recursive: 是否递归处理子文件夹
  62. """
  63. # 支持的 MXD 文件扩展名
  64. extensions = ['.mxd', '.MXD']
  65. # 收集所有 MXD 文件
  66. mxd_files = []
  67. if recursive:
  68. for root, dirs, files in os.walk(folder_path):
  69. for file in files:
  70. if os.path.splitext(file)[1] in extensions:
  71. mxd_files.append(os.path.join(root, file))
  72. else:
  73. for file in os.listdir(folder_path):
  74. if os.path.splitext(file)[1] in extensions:
  75. mxd_files.append(os.path.join(folder_path, file))
  76. print("找到 {len(mxd_files)} 个 MXD 文件")
  77. # 处理每个 MXD 文件
  78. success_count = 0
  79. for mxd_file in mxd_files:
  80. if update_sde_connection_in_mxd(mxd_file, old_ip, new_ip):
  81. success_count += 1
  82. print("\n处理完成!成功处理 {success_count}/{len(mxd_files)} 个文件")
  83. def update_sde_connection_file(connection_file, old_ip, new_ip):
  84. """
  85. 直接更新 SDE 连接文件 (.sde)
  86. """
  87. try:
  88. # 读取连接文件内容
  89. with open(connection_file, 'r') as f:
  90. content = f.read()
  91. # 替换 IP 地址
  92. new_content = content.replace(old_ip, new_ip)
  93. # 保存文件
  94. with open(connection_file, 'w') as f:
  95. f.write(new_content)
  96. print("已更新连接文件: {os.path.basename(connection_file)}")
  97. return True
  98. except Exception as e:
  99. print("更新连接文件失败: {str(e)}")
  100. return False
  101. # ==================== 使用示例 ====================
  102. if __name__ == "__main__":
  103. # 示例 1: 处理单个 MXD 文件
  104. print("=== 示例 1: 处理单个 MXD 文件 ===")
  105. # update_sde_connection_in_mxd(
  106. # r"C:\Project\maps\project.mxd",
  107. # "192.168.1.100", # 旧 IP
  108. # "10.255.133.176" # 新 IP
  109. # )
  110. # 示例 2: 批量处理文件夹
  111. print("\n=== 示例 2: 批量处理文件夹 ===")
  112. batch_update_sde_connections(
  113. r"C:\TEMP\1", # 文件夹路径
  114. "192.168.100.252", # 旧 IP
  115. "192.168.60.20", # 新 IP
  116. recursive=True # 递归处理子文件夹
  117. )