Python:从几个XLSX文件中创建几个XML文件
我发现了如何使用此代码从单个XLSX文件中创建一个Singe XML文件:
from openpyxl import load_workbook
wb = load_workbook("convert_file.xlsx",data_only=True)
ws = wb.active
sheetname_row_list:list =[]
for row in ws.iter_rows(min_row=2, max_row=7, min_col=1, max_col=6):
row = [cell.value for cell in row]
sheetname_row_list.append(row)
from yattag import Doc, indent
doc, tag, text = Doc().tagtext()
for row in sheetname_row_list:
with tag("column0"):
text(row[0])
with tag("column1"):
text(row[1])
with tag("column2"):
text(row[2])
with tag("column3"):
text(row[3])
with tag("column4"):
text(row[4])
with tag("column5"):
text(row[5])
result = indent(
doc.getvalue(),
indentation = ' ',
indent_text = False
)
with open("result_file.xml","w") as f:
f.write(result)
现在如何从具有所有相同结构的几个XLSX文件中创建多个XML文件?
我有“ convert_file.xlsx” /“ convert_file_2.xlsx” /“ convert_file_3.xlsx” ...并想要获得“ result_file.xml” /“ result_file_2.xml” /“ result_file_2.xml” /“ result_file_3.xml” ...
I found out how to create a singe xml-file from a single xlsx-file using this code:
from openpyxl import load_workbook
wb = load_workbook("convert_file.xlsx",data_only=True)
ws = wb.active
sheetname_row_list:list =[]
for row in ws.iter_rows(min_row=2, max_row=7, min_col=1, max_col=6):
row = [cell.value for cell in row]
sheetname_row_list.append(row)
from yattag import Doc, indent
doc, tag, text = Doc().tagtext()
for row in sheetname_row_list:
with tag("column0"):
text(row[0])
with tag("column1"):
text(row[1])
with tag("column2"):
text(row[2])
with tag("column3"):
text(row[3])
with tag("column4"):
text(row[4])
with tag("column5"):
text(row[5])
result = indent(
doc.getvalue(),
indentation = ' ',
indent_text = False
)
with open("result_file.xml","w") as f:
f.write(result)
How can I now create several XML files from several XLSX files with all the same structure?
I have "convert_file.xlsx" / "convert_file_2.xlsx" / "convert_file_3.xlsx"... and want to get "result_file.xml" / "result_file_2.xml" / "result_file_3.xml"...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用pathlib和类似的东西:
快乐的编码。
编辑:包括一种将文件扩展名从.xlsx更改为.xml 的方法
I suggest using pathlib and something like:
Happy coding.
Edit: Included a way of changing the file extension from .xlsx to .xml