使用python将XLSX正确转换为CSV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20105118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:31:59  来源:igfitidea点击:

Convert XLSX to CSV correctly using python

pythonexcelpython-2.7csv

提问by geogeek

I am looking for a python library or any help to convert .XLSX files to .CSV files.

我正在寻找一个 python 库或任何将 .XLSX 文件转换为 .CSV 文件的帮助。

采纳答案by Hemant

Read your excel using the xlrdmodule and then you can use the csvmodule to create your own csv.

使用该xlrd模块读取您的 excel ,然后您可以使用该csv模块创建您自己的 csv。

Install the xlrd module in your command line:

在命令行中安装 xlrd 模块:

pip install xlrd

pip install xlrd

Python script:

蟒蛇脚本:

import xlrd
import csv

def csv_from_excel():
    wb = xlrd.open_workbook('excel.xlsx')
    sh = wb.sheet_by_name('Sheet1')
    your_csv_file = open('your_csv_file.csv', 'w')
    wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)

    for rownum in range(sh.nrows):
        wr.writerow(sh.row_values(rownum))

    your_csv_file.close()

# runs the csv_from_excel function:
csv_from_excel()