将 Excel 文件文件夹转换为 CSV 文件/合并 Excel 工作簿 [英] Converting a folder of Excel files into CSV files/Merge Excel Workbooks

查看:49
本文介绍了将 Excel 文件文件夹转换为 CSV 文件/合并 Excel 工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量 Excel 工作簿的文件夹.有没有办法使用 Python 的 xlrd、xlutiles 和 xlsxWriter 将此文件夹中的每个文件转换为 CSV 文件?

I have a folder with a large number of Excel workbooks. Is there a way to convert every file in this folder into a CSV file using Python's xlrd, xlutiles, and xlsxWriter?

我希望新转换的 CSV 文件的扩展名为_convert.csv".

I would like the newly converted CSV files to have the extension '_convert.csv'.

否则...

有没有办法将文件夹中的所有 Excel 工作簿合并成一个大文件?

Is there a way to merge all the Excel workbooks in the folder to create one large file?

我一直在寻找两种方法都可以,但没有任何效果...

I've been searching for ways to do both, but nothing has worked...

推荐答案

使用 pywin32,这将找到指定目录中的所有 .xlsx 文件,并将它们打开并重新保存为 .csv.使用 pywin32 找出正确的命令相对容易……只需记录一个 Excel 宏并手动执行打开/保存,然后查看生成的宏.

Using pywin32, this will find all the .xlsx files in the indicated directory and open and resave them as .csv. It is relatively easy to figure out the right commands with pywin32...just record an Excel macro and perform the open/save manually, then look at the resulting macro.

import os
import glob
import win32com.client

xl = win32com.client.gencache.EnsureDispatch('Excel.Application')

for f in glob.glob('tmp/*.xlsx'):
    fullname = os.path.abspath(f)
    xl.Workbooks.Open(fullname)
    xl.ActiveWorkbook.SaveAs(Filename=fullname.replace('.xlsx','.csv'),
                             FileFormat=win32com.client.constants.xlCSVMSDOS,
                             CreateBackup=False)
    xl.ActiveWorkbook.Close(SaveChanges=False)

这篇关于将 Excel 文件文件夹转换为 CSV 文件/合并 Excel 工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆