Python:从几个XLSX文件中创建几个XML文件

发布于 2025-02-13 14:54:57 字数 1162 浏览 1 评论 0原文

我发现了如何使用此代码从单个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2025-02-20 14:54:57

我建议使用pathlib和类似的东西:

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 的方法

I suggest using pathlib and something like:

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
   ...

Happy coding.

Edit: Included a way of changing the file extension from .xlsx to .xml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文