Products
GG网络技术分享 2025-11-13 12:16 1
要将Excel数据转换为XML格式并保存到文件中,Neng用Python编程语言来实现。
python import xlrd from xml.dom import minidom

def readexcel: workbook = xlrd.openworkbook sheet = workbook.sheetbyname nrows = sheet.nrows data = for i in range: rowdata = for j in range: rowdata.append) data.append return data
def createxml: doc = minidom.Document root = doc.createElement doc.appendChild for rowdata in data: row = doc.createElement root.appendChild for j, cellvalue in enumerate: cell = doc.createElement celltext = doc.createTextNode) cell.appendChild row.appendChild return doc.toprettyxml
def write_xml: with open as f: f.write
filepath = "/path/to/file.xlsx" sheetname = "Sheet1" xmlfilepath = "/path/to/file.xml"
data = readexcel xmlstr = createxml writexml
在这玩意儿示例中:
read_excel 函数打开指定的Excel文件, 读取指定干活表中的数据,并将其作为二维列表返回。create_xml 函数收下一个二维列表作为输入, 创建一个XML文档,将每行数据转换为XML中的row元素,并将个个单元格值转换为cell元素。write_xml 函数将生成的XML字符串写入到指定的文件路径。请确保替换file_path和sheet_name变量为实际的文件路径和干活表名称。这段代码将Excel文件中的数据转换为XML格式,并将其保存到指定的文件路径中。
Demand feedback