在XML文件夹上运行python代码

时间:2015-06-16 20:45:33

标签: python xml batch-file scripting directory

我希望能够在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")

1 个答案:

答案 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