截至 2021 年 12 月和 Python 3:
openpyxl
API 已经发生了很大的变化(请参阅https://openpyxl.readthedocs.io/en/stable/usage.html),我已经通过@Boud(现在是@Zeugma?)更新了这部分答案,如下所示:
import openpyxl
import csv
wb = openpyxl.load_workbook('test.xlsx')
sh = wb.active # was .get_active_sheet()
with open('test.csv', 'w', newline="") as file_handle:
csv_writer = csv.writer(file_handle)
for row in sh.iter_rows(): # generator; was sh.rows
csv_writer.writerow([cell.value for cell in row])
@Leonid 提出了一些有用的评论 - 特别是:
csv.writer
提供了一些额外的选项,例如自定义分隔符:
csv_writer = csv.writer(fout, delimiter='|', quotechar='"', quoting=csv.QUOTE_MINIMAL)
高温高压