目的:通过python修改xml配置文件,并将python脚本编译成可执行文件
一、通过python修改xml配置文件
如下是待修改的EAICfg.xml
<?xml version='1.0' encoding='utf-8'?>
<root>
<subRoot>
<testService>
<publishProfile profileName="Contoso - FTP" publishMethod="FTP" publishUrl="114.114.114.114" ftpPassiveMode="True" userName="some_ftp_user" userPWD="some_ftp_pwd" destinationAppUrl="http://site.server.contoso.com/" SQLServerDBConnectionString="server=contoso-mssql;uid=db_user;pwd=db_pwd;database=some_db" mySQLDBConnectionString="server=contoso-mysql;uid=db_user;pwd=db_pwd;database=some_db" hostingProviderForumLink="http://support.contoso.com/" controlPanelLink="http://controlpanel.contoso.com/" />
<comment key="sss" ip="huhu" attr="old" />
</testService>
</subRoot>
<testService2>
<publishProfile profileName="Contoso - FTP" publishMethod="FTP" publishUrl="0000" ftpPassiveMode="True" userName="some_ftp_user" userPWD="some_ftp_pwd" destinationAppUrl="http://site.server.contoso.com/" SQLServerDBConnectionString="server=contoso-mssql;uid=db_user;pwd=db_pwd;database=some_db" mySQLDBConnectionString="server=contoso-mysql;uid=db_user;pwd=db_pwd;database=some_db" hostingProviderForumLink="http://support.contoso.com/" controlPanelLink="http://controlpanel.contoso.com/" />
<comment key="sss" ip="000" attr="000" />
<comment key="sss" ip="000" attr="000" />
<comment key="aaa" ip="000" attr="newAttr" />
<comment ip="000" attr="000" />
</testService2>
</root>
python提供了xml.etree.ElementTree API,用于解析和创建XML数据
如下是用于修改xml文件的相应代码test.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import xml.etree.ElementTree as ET
# 修改本地xml文件
def change_xml(xml_path, change_list):
'''
修改本地xml文件
xml_path:xml路径
change_list:需要修改内容,
[{'xml_json_path': 'json_path路径', 'data':{'元素属性名称':'元素属性值'}}]
例子:
[{'xml_json_path': './/property[@name = "ReportName"]', 'data':{'value':'666955555'}}]
'''
print(xml_path)
# 打开xml文件
doc = ET.parse(xml_path)
root = doc.getroot()
for change in change_list:
# 查找修改路径
for sub1 in root.findall(change['xml_json_path']):
# 修改标签内容
for key, value in change['data'].items():
# 修改内容
sub1.set(key, value)
# 保存修改
doc.write(xml_path)
doc.write(xml_path, encoding='utf-8', xml_declaration=True)
# change_xml('EAICfg.xml',[{'xml_json_path': 'testService2/comment', 'data':{'ip':'114.114.114.114','attr':'newAttr'}},
# {'xml_json_path': 'subRoot/testService/publishProfile[@publishMethod="FTP"]', 'data':{'publishUrl':'114.114.114.114'}}])
#为避免可执行文件报错找不到路径下的文件,需要用绝对路径
change_xml('/Users/yuhuan/Documents/MyProjects/EAICfg.xml',[{'xml_json_path': 'testService2/comment[@key]', 'data':{'ip':'114.114.114.114','attr':'newAttr'}},
{'xml_json_path': 'subRoot/testService/publishProfile[@publishMethod="FTP"]', 'data':{'publishUrl':'114.114.114.114'}}])
# change_xml('EAICfg.xml',[{'xml_json_path': 'testService2/comment[@key="sss"]', 'data':{'ip':'114.114.114.114','attr':'newAttr'}},
# {'xml_json_path': 'subRoot/testService/publishProfile[@publishMethod="FTP"]', 'data':{'publishUrl':'114.114.114.114'}}])
二、将python脚本编译成可执行文件
首先需要安装pyinstaller库
PyInstaller支持在在Windows/Linux/Mac环境下将Python脚本打包成可执行程序,在没有Python环境的机器上运行。注意:需要在哪个操作系统平台一运行,需在相应的操作系统(或虚拟机)下编译。(即exe文件必须要在windows操作系统下编译)
pip install pyinstaller
进入test.py所在文件夹
pyinstaller -F test.py
常用参数:
-F 生成单个的exe文件。
-w 隐藏运行窗口。
-h 可以查看帮助信息,更多指令可以通过这个查看。
生成的可执行文件在dist目录下。
![](https://img.haomeiwen.com/i6555104/90f22bbffc94a8f3.png)
大功告成!
附录:python库pyinstaller的离线安装
联网情况下,根据pyinstaller安装教程-官方即可完成pyinstaller的安装。
pip install pyinstaller
若要安装pyintaller的主机为公司内网环境,无法连接外网,我最早尝试了官方教程中如下通过下载git源码方式,亲测无法实现离线安装,因为录入pip install .之后会网络请求下载其他依赖
git clone https://github.com/pyinstaller/pyinstaller
cd pyinstaller
pip install .
因为安装pyinstaller之前需要完成其他所有依赖包的安装,只下载pyinstaller代码是不够的。这时找到一位同病相怜的博主的分享救了老命!!参考
由于最新官方已经不推荐 python setup.py install 这种做法了,实际可行的终极解决方案如下
内网环境安装:
-
首先需要到外网机上下载好所有需要的离线安装包,命令为: pip download 包名 -d 下载路径。后面的-d 和下载路径可加可不加,不加就是下载到cmd键入命令时的那个目录下。比如:pip download altgraph -d c:/ ,就是把altgraph下载到c盘的根目录下。需要注意的是,命令pip download pyinstaller,会直接把pyinstaller和安装pyinstaller需要的其他依赖包都下载下来。
注意:需要在内网哪个操作系统实际安装,外网也要以同样的操作系统pip download相应包。(mac和windows执行pip download pyinstaller 下载的依赖和安装包版本是不一样的) -
下载好后把所有的包传到内网机,然后一个一个 pip install 加包名即可,注意安装的顺序,需要先安装依赖包,最后安装下划线的pyinstaller包。
pip install XXXXXXX.whl
![](https://img.haomeiwen.com/i6555104/b49103bbcfe1f139.png)
3.验证安装是否成功
pyinstaller --version
网友评论