网站建设个人博客,wordpress栏目对应模板,河南网站优化排名,南靖企业网站建设公司Python 实现 Excel 到 CSV 的转换程序
Excel 可以将电子表格保存为 CSV 文件#xff0c;只要点几下 鼠标#xff0c;但如果有几百个 Excel文件要转换为 CSV #xff0c; 就需要点击几小时。利用 openpyxl 模块#xff0c; 编程读取当前工作目录中的所有 Excel 文件#x…Python 实现 Excel 到 CSV 的转换程序
Excel 可以将电子表格保存为 CSV 文件只要点几下 鼠标但如果有几百个 Excel文件要转换为 CSV 就需要点击几小时。利用 openpyxl 模块 编程读取当前工作目录中的所有 Excel 文件并输出 为 CSV 文件。
一个 Excel 文件可能包含多个工作表必须为每个表 创建一个 CSV 文件。CSV 文件的文件名应该是 Excel 文件名_表标题.csv ,其中 Excel文件名 是没有扩展名的 Excel 文件名例如 ‘spam_data’ , 而不是 ‘spam_data.xlsx’ ), 是 Worksheet 对象的 title 变量中的字符串。
该程序将包含许多嵌套的 for 循环。 该程序的框架看起来像这样 代码样例 import os for excelFile in os.listdir(‘.’): # Skip non-xlsx files, load the workbook object. for sheetName in wb.get_sheet_names(): # Loop through every sheet in the workbook, sheet wb.get_sheet_by_name(sheetName) # Create the CSV filename from the Excel filename and sheet title.# Create the csv.writer object for this CSV file.# Loop through every row in the sheet.for rowNum in range(1,sheet.get_highest_row() 1):rowData [] # append each cell to this list#Loop through each cell in the row.for colNum in range(1, sheet.get_highest_column() 1):# Append each cells data to rowData.# Write the rowData list to the CSV file.csvFile.close()