如何使用Python将多个Excel文件转换为CSV UTF-8编码
我在同一目录中有30+ xlsx
文件,并且使用 python
我想使用UTF-8编码将所有文件转换为CSV,无论文件中是否存在任何编码。我正在使用python的魔术库获取文件名(下面代码)。对于转换,我尝试了so用户julian there (我使用了发布的代码“ InvalidFileException:OpenPyXl不支持文件格式,请检查您可以首先使用Excel打开。代码>。
谢谢您的帮助
# import a library to detect encodings
import magic
import glob
print("File".ljust(45), "Encoding")
for filename in glob.glob('path*.xlsx'):
with open(filename, 'rb') as rawdata:
result = magic.from_buffer(rawdata.read(2048))
print(filename.ljust(45), result)
!
from openpyxl import load_workbook
import csv
from os import sys
def get_all_sheets(excel_file):
sheets = []
workbook = load_workbook(excel_file,read_only=True,data_only=True)
all_worksheets = workbook.get_sheet_names()
for worksheet_name in all_worksheets:
sheets.append(worksheet_name)
return sheets
def csv_from_excel(excel_file, sheets):
workbook = load_workbook(excel_file,data_only=True)
for worksheet_name in sheets:
print("Export " + worksheet_name + " ...")
try:
worksheet = workbook.get_sheet_by_name(worksheet_name)
except KeyError:
print("Could not find " + worksheet_name)
sys.exit(1)
your_csv_file = open(''.join([worksheet_name,'.csv']), 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for row in worksheet.iter_rows():
lrow = []
for cell in row:
lrow.append(cell.value)
wr.writerow(lrow)
print(" ... done")
your_csv_file.close()
if not 2 <= len(sys.argv) <= 3:
print("Call with " + sys.argv[0] + " <xlxs file> [comma separated list of sheets to export]")
sys.exit(1)
else:
sheets = []
if len(sys.argv) == 3:
sheets = list(sys.argv[2].split(','))
else:
sheets = get_all_sheets(sys.argv[1])
assert(sheets != None and len(sheets
) > 0)
csv_from_excel(sys.argv[1], sheets)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试使用
pandas
库?您可以使用OS
将所有文件存储在列表中。然后,您可以使用read_excel
循环浏览列表,然后打开每个excel
文件,然后写入csv
。因此看起来会像这样:不是最好的,但应该满足您的需求
Have you tried to use
Pandas
library? You can store all the files in a list usingos
. You can then loop through the list and open eachExcel
file usingread_excel
and then write to acsv
. So it will look something like this:Not the best but should meet your needs
首先,第一个错误很明显:
InvalidFileException:OpenPyXl不支持文件格式,请检查您可以首先使用Excel打开它。
Excel是否成功打开了此文件?如果是,我们需要工作簿(或其中一小部分)。
第二个问题的答案:
您也可以明确指定 csv的方言 AS 参数。
In first, the first error is obvious:
InvalidFileException: openpyxl does not support file format, please check you can open it with Excel first.
Does Excel successfully open this file? If yes, we need the workbook (or small part of it).
The answer to the second question:
Also you can explicitly specify the dialect of csv as csv.writer parameter.