我希望能够在XML文件的文件夹上运行以下代码,而不是单个文件。我也不想为每个文件更改xmlfile = 'test.xml'
行100次以上。
这是我发现并正在测试的示例elementTree
代码。
from openpyxl import Workbook
import xml.etree.ElementTree as ET
xmlfile = 'test.xml'
element_tree = ET.parse(xmlfile)
root = element_tree.getroot()
agreement = root.find(".//tag").text
print (agreement)
wb = Workbook()
kevin = ["1", "2", "3"]
# grab the active worksheet
ws = wb.active
# Data can be assigned directly to cells
ws['A1'] = 42
# Rows can also be appended
ws.append[(agreement)]
ws.append(kevin)
# Save the file
wb.save("sample.xlsx")
答案 0 :(得分:0)
最好配合这个。
import os
import fnmatch
#os.getcwd() or a specific path of the need
path = os.getcwd() + '/open'
mee = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(path)
for f in fnmatch.filter(files, '*.xml')]
for ffile in mee:
#do other stuff