【发布时间】:2021-11-26 11:30:54
【问题描述】:
我有一个工具可以让 ssers 上传大型 xlsx 文件。我们需要将这些 xlsx 文件转换为 csv 以进行处理。但是,我没有找到将 xlsx 文件转换为 csv 的快速方法。我们无法使用 VBS 脚本(速度非常快)。我尝试了各种方式,比如pandas、openpyxl:
熊猫
read_file = pd.read_excel(os.path.join(path, old_filename), engine="openpyxl")
read_file.to_csv(os.path.join(path, new_filename), index=None, header=True)
openpyxl
wb = openpyxl.load_workbook(file, data_only=True)
sh = wb.active # was .get_active_sheet()
with open(os.path.join(path, filename), 'w', newline="") as f:
c = csv.writer(f)
for r in sh.iter_rows(): # generator; was sh.rows
c.writerow([cell.value for cell in r])
但是一个 60mb 的 xlsx 文件大约需要 4 分钟才能将其转换为 csv。
有没有办法让转换更快?我愿意接受任何解决方案。
【问题讨论】: