如何将Excel工作表另存为CSV
发布于 2021-01-29 15:07:03
关注者
0
被浏览
91
1 个回答
-
使用这两个库的最基本示例逐行描述:
- 打开xls工作簿
- 引用第一个电子表格
- 用二进制打开写入目标csv文件
- 创建默认的csv writer对象
- 循环遍历第一个电子表格的所有行
- 将行转储到csv中
import xlrd import csv with xlrd.open_workbook('a_file.xls') as wb: sh = wb.sheet_by_index(0) # or wb.sheet_by_name('name_of_the_sheet_here') with open('a_file.csv', 'wb') as f: # open('a_file.csv', 'w', newline="") for python 3 c = csv.writer(f) for r in range(sh.nrows): c.writerow(sh.row_values(r))
import openpyxl import csv wb = openpyxl.load_workbook('test.xlsx') sh = wb.get_active_sheet() with open('test.csv', 'wb') as f: # open('test.csv', 'w', newline="") for python 3 c = csv.writer(f) for r in sh.rows: c.writerow([cell.value for cell in r])