我了解了如何使用以下代码从单个xlsx文件创建singe文件:
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_3.xml”.
发布于 2022-07-07 07:15:49
我建议使用路径库,类似于:
from pathlib import Path
source_directory = Path('.') # Pointing to the XLSX files
for filename in source_directory.glob('*.xlsx'):
# Call to your conversion code
...
resultname = f'{filename.stem.replace("convert", "result")}.xml'
# Save to file using resultname
...编码愉快。
编辑:包括一种将文件扩展名从.xlsx更改为.xml的方法
https://stackoverflow.com/questions/72893265
复制相似问题