Save XML file to local system

使用XML与FSO保存文件
本文介绍了一种使用XML DOM对象与FileSystemObject (FSO)来保存XML文件的方法。当发现XML DOM对象的save方法在客户端不起作用时,作者采用FSO创建文本文件并利用Element的xml属性获取XML内容,但这种方法限定了文件只能使用ASCII或Unicode编码。
 
Off course, this operation need user’s permission, but when I using xml_dom_obj.save(), nothing happened, even a permission requesting dialog.
I donot know if it was blocked by my IE browser or the save method just can not work at the client side.
so I found another way to do this.
use FileSystemObject to save my xml file, and use Element.xml property to get my xml content.
Because of FileSystemObject.CreateTextFile() just support two charset (ASCII, Unicode), so your xml files will be limited in the 2 encodings.
 
 1
 2 <script type="text/javascript">
 3
 4 function verify()
 5 { 
 6     if (xmlDoc.readyState != 4)
 7     { 
 8         return false;
 9     }
10 }
11
12 var browserok=window.ActiveXObject
13 if (browserok)
14 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
15 xmlDoc.async="false";
16 xmlDoc.onreadystatechange=verify;
17 xmlDoc.load('hello.xml');
18 // Gets a reference to the root node of the document.
19 root_node=xmlDoc.documentElement;
20
21 var fso = new ActiveXObject("Scripting.FileSystemObject")
22
23 // object.CreateTextFile(filename[, overwrite[, unicode]])
24 // 3rd param
25 // The value is true if the file is created as a Unicode file,
26 // false if it's created as an ASCII file
27 var tf = fso.CreateTextFile("output.xml", true, true);
28
29 tf.WriteLine("<?xml version=/"1.0/" encoding=/"UTF-16/"?>")
30 tf.write(root_node.xml)
31 tf.close()
32
33 alert("ok")
34
35 </script>
 
[Running] python -u "e:\system\Desktop\项目所需文件\工具\ffff\Diff Analyzer.py" Traceback (most recent call last): File "e:\system\Desktop\\u9879�ڏ�������\�H��\ffff\Diff Analyzer.py", line 19, in safe_read_excel df = pd.read_excel( ^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 495, in read_excel io = ExcelFile( ^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 1567, in __init__ self._reader = self._engines[engine]( ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_xlrd.py", line 46, in __init__ super().__init__( File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 573, in __init__ self.book = self.load_workbook(self.handles.handle, engine_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_xlrd.py", line 63, in load_workbook return open_workbook(file_contents=data, **engine_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\xlrd\__init__.py", line 170, in open_workbook raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported') xlrd.biffh.XLRDError: Excel xlsx file; not supported Traceback (most recent call last): File "e:\system\Desktop\\u9879�ڏ�������\�H��\ffff\Diff Analyzer.py", line 19, in safe_read_excel df = pd.read_excel( ^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 495, in read_excel io = ExcelFile( ^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 1567, in __init__ self._reader = self._engines[engine]( ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_xlrd.py", line 46, in __init__ super().__init__( File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 573, in __init__ self.book = self.load_workbook(self.handles.handle, engine_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_xlrd.py", line 63, in load_workbook return open_workbook(file_contents=data, **engine_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\xlrd\__init__.py", line 170, in open_workbook raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported') xlrd.biffh.XLRDError: Excel xlsx file; not supported Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 293, in read self.read_workbook() File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 153, in read_workbook self.parser.parse() File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\workbook.py", line 46, in parse package = WorkbookPackage.from_tree(node) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\serialisable.py", line 83, in from_tree obj = desc.from_tree(el) ^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\sequence.py", line 94, in from_tree return [self.expected_type.from_tree(el) for el in node] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\serialisable.py", line 103, in from_tree return cls(**attrib) ^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\workbook\views.py", line 133, in __init__ self.guid = guid ^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\base.py", line 257, in __set__ raise ValueError('Value does not match pattern {0}'.format(self.pattern)) ValueError: Value does not match pattern {[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\} The above exception was the direct cause of the following exception: Traceback (most recent call last): File "e:\system\Desktop\\u9879�ڏ�������\�H��\ffff\Diff Analyzer.py", line 19, in safe_read_excel df = pd.read_excel( ^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 495, in read_excel io = ExcelFile( ^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 1567, in __init__ self._reader = self._engines[engine]( ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_openpyxl.py", line 553, in __init__ super().__init__( File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 573, in __init__ self.book = self.load_workbook(self.handles.handle, engine_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_openpyxl.py", line 572, in load_workbook return load_workbook( ^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 348, in load_workbook reader.read() File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 309, in read raise ValueError( ValueError: Unable to read workbook: could not read workbook from E:\system\Desktop\work\CANBIT\New\D59T(BEV_HCU��)-00-04.xlsx. This is most probably because the workbook source files contain some invalid XML. Please see the exception for more details. Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\compat\_optional.py", line 135, in import_optional_dependency module = importlib.import_module(name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\importlib\__init__.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1387, in _gcd_import File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked ModuleNotFoundError: No module named 'odf' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "e:\system\Desktop\\u9879�ڏ�������\�H��\ffff\Diff Analyzer.py", line 19, in safe_read_excel df = pd.read_excel( ^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 495, in read_excel io = ExcelFile( ^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 1567, in __init__ self._reader = self._engines[engine]( ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_odfreader.py", line 49, in __init__ import_optional_dependency("odf") File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\compat\_optional.py", line 138, in import_optional_dependency raise ImportError(msg) ImportError: Missing optional dependency 'odfpy'. Use pip or conda to install odfpy. Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 293, in read self.read_workbook() File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 153, in read_workbook self.parser.parse() File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\workbook.py", line 46, in parse package = WorkbookPackage.from_tree(node) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\serialisable.py", line 83, in from_tree obj = desc.from_tree(el) ^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\sequence.py", line 94, in from_tree return [self.expected_type.from_tree(el) for el in node] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\serialisable.py", line 103, in from_tree return cls(**attrib) ^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\workbook\views.py", line 133, in __init__ self.guid = guid ^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\base.py", line 257, in __set__ raise ValueError('Value does not match pattern {0}'.format(self.pattern)) ValueError: Value does not match pattern {[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\} The above exception was the direct cause of the following exception: Traceback (most recent call last): File "e:\system\Desktop\\u9879�ڏ�������\�H��\ffff\Diff Analyzer.py", line 19, in safe_read_excel df = pd.read_excel( ^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 495, in read_excel io = ExcelFile( ^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 1567, in __init__ self._reader = self._engines[engine]( ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_openpyxl.py", line 553, in __init__ super().__init__( File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_base.py", line 573, in __init__ self.book = self.load_workbook(self.handles.handle, engine_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\io\excel\_openpyxl.py", line 572, in load_workbook return load_workbook( ^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 348, in load_workbook reader.read() File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 309, in read raise ValueError( ValueError: Unable to read workbook: could not read workbook from E:\system\Desktop\work\CANBIT\New\D59T(BEV_HCU��)-00-04.xlsx. This is most probably because the workbook source files contain some invalid XML. Please see the exception for more details. Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 293, in read self.read_workbook() File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 153, in read_workbook self.parser.parse() File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\workbook.py", line 46, in parse package = WorkbookPackage.from_tree(node) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\serialisable.py", line 83, in from_tree obj = desc.from_tree(el) ^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\sequence.py", line 94, in from_tree return [self.expected_type.from_tree(el) for el in node] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\serialisable.py", line 103, in from_tree return cls(**attrib) ^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\workbook\views.py", line 133, in __init__ self.guid = guid ^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\base.py", line 257, in __set__ raise ValueError('Value does not match pattern {0}'.format(self.pattern)) ValueError: Value does not match pattern {[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\} The above exception was the direct cause of the following exception: Traceback (most recent call last): File "e:\system\Desktop\\u9879�ڏ�������\�H��\ffff\Diff Analyzer.py", line 38, in safe_read_excel wb = load_workbook(file_path, read_only=True, data_only=True, keep_vba=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 348, in load_workbook reader.read() File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\reader\excel.py", line 309, in read raise ValueError( ValueError: Unable to read workbook: could not read workbook from E:\system\Desktop\work\CANBIT\New\D59T(BEV_HCU��)-00-04.xlsx. This is most probably because the workbook source files contain some invalid XML. Please see the exception for more details. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "e:\system\Desktop\\u9879�ڏ�������\�H��\ffff\Diff Analyzer.py", line 282, in <module> comparison_result = compare_excel(old_excel, new_excel) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "e:\system\Desktop\\u9879�ڏ�������\�H��\ffff\Diff Analyzer.py", line 60, in compare_excel df_new = safe_read_excel(new_file, skiprows=9) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "e:\system\Desktop\\u9879�ڏ�������\�H��\ffff\Diff Analyzer.py", line 53, in safe_read_excel raise ValueError(f"�ٖ@\u8bfb�敶�� {file_path}: {str(e)}") ValueError: �ٖ@\u8bfb�敶�� E:\system\Desktop\work\CANBIT\New\D59T(BEV_HCU��)-00-04.xlsx: Unable to read workbook: could not read workbook from E:\system\Desktop\work\CANBIT\New\D59T(BEV_HCU��)-00-04.xlsx. This is most probably because the workbook source files contain some invalid XML. Please see the exception for more details. 开始比较Excel文件... 正在读取旧文件: E:\system\Desktop\work\CANBIT\Old\D13N(CONV)-00-01(KS)(MET).xlsx 尝试使用引擎: xlrd 引擎 xlrd 失败: Excel xlsx file; not supported 尝试使用引擎: openpyxl 成功使用引擎: openpyxl 正在读取新文件: E:\system\Desktop\work\CANBIT\New\D59T(BEV_HCU無)-00-04.xlsx 尝试使用引擎: xlrd 引擎 xlrd 失败: Excel xlsx file; not supported 尝试使用引擎: openpyxl 引擎 openpyxl 失败: Unable to read workbook: could not read workbook from E:\system\Desktop\work\CANBIT\New\D59T(BEV_HCU無)-00-04.xlsx. This is most probably because the workbook source files contain some invalid XML. Please see the exception for more details. 尝试使用引擎: odf 引擎 odf 失败: Missing optional dependency 'odfpy'. Use pip or conda to install odfpy. 尝试使用引擎: auto 引擎 auto 失败: Unable to read workbook: could not read workbook from E:\system\Desktop\work\CANBIT\New\D59T(BEV_HCU無)-00-04.xlsx. This is most probably because the workbook source files contain some invalid XML. Please see the exception for more details. 所有引擎失败,尝试手动提取数据 比较过程中发生错误: 无法读取文件 E:\system\Desktop\work\CANBIT\New\D59T(BEV_HCU無)-00-04.xlsx: Unable to read workbook: could not read workbook from E:\system\Desktop\work\CANBIT\New\D59T(BEV_HCU無)-00-04.xlsx. This is most probably because the workbook source files contain some invalid XML. Please see the exception for more details. 详细错误信息: [Done] exited with code=0 in 1.782 seconds
最新发布
09-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值