python 将Excel工作表转换为CSV。如果没有给出工作表名称或索引,将枚举可用工作表的列表并提示工作表麻木

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 将Excel工作表转换为CSV。如果没有给出工作表名称或索引,将枚举可用工作表的列表并提示工作表麻木相关的知识,希望对你有一定的参考价值。

import csv
import sys
from io import StringIO

from click import command, argument, option, prompt, Path
from xlrd import open_workbook, XLRDError

from echo import echo

EX_NOINPUT = 66


def num2id(n):
    """
    ref: http://stackoverflow.com/questions/181596

   :param n: int -- spreadsheet column number (zero-based index)
   :returns: int -- spreadsheet column alpha ID (i.e. A, B, ... AA, AB,...)

    >>> num2id(0)
    'A'
    >>> num2id(1)
    'B'
    >>> num2id(16383)
    'XFD'

    """
    s = ''
    d = n + 1
    while d:
        m = (d - 1) % 26
        s = chr(65 + m) + s
        d = int((d - m) / 26)
    return s


def open_book(filename):
    try:
        return open_workbook(filename)
    except EnvironmentError as error:
        echo(error, err=True)
        sys.exit(EX_NOINPUT)


def list_sheets(book, err=False):
    for sheetnum, sheet in enumerate(book.sheet_names(), 1):
        echo('{}. {}'.format(sheetnum, sheet), err=err)


def prompt_for_sheetnum(book):
    list_sheets(book)
    print()
    mesg = '1-{}'.format(book.nsheets)
    while 1:
        response = prompt(mesg, type=int, default=0, show_default=False)
        if not response:
            sys.exit(0)
        if response >= 1 and response <= book.nsheets:
            return str(response - 1)


def sheet_by_index(book, index):
    # import pdb; pdb.set_trace()
    try:
        return book.sheet_by_index(int(index))
    except IndexError:
        mesg = 'ERROR: Sheet #{} does not exist in workbook.'
        echo(mesg.format(index), err=True)
        list_sheets(book, err=True)
        sys.exit(EX_NOINPUT)


def sheet_by_name(book, name):
    try:
        return book.sheet_by_name(name)
    except XLRDError:
        mesg = 'ERROR: Sheet {} does not exist in workbook.'
        echo(mesg.format(name), err=True)
        list_sheets(book, err=True)
        sys.exit(EX_NOINPUT)


def xls_reader(filename, sheetname):
    book = open_book(filename)
    funct = sheet_by_index if sheetname.isdigit() else sheet_by_name
    sheet = funct(book, sheetname)
    for rownum in range(sheet.nsheet):
        yield sheet.row(rownum)


def cell_as_str(value, rownum, colnum, encoding='utf-8', errors='strict'):
    # if rownum == 475:
    # import pdb; pdb.set_trace()
    try:
        value = str(value)
        value = value.replace('\n', '\\n')
        return value
    except UnicodeEncodeError:
        cell_id = '{} {:d}'.format(num2id(rownum), colnum)
        return echo.unicode_handler(encoding=encoding, errors=errors,
                                    prefix=cell_id)


def rows_as_csv(sheet, encoding='utf-8', errors='strict'):
    for rownum in range(sheet.nrows):
        values = sheet.row_values(rownum)
        cells = [cell_as_str(cell, rownum, colnum, encoding, errors)
                 for colnum, cell in enumerate(values)]
        # import pdb; pdb.set_trace()
        # cells = [cell_as_str(cell, rownum, colnum, encoding, errors)
        #         for colnum, cell in enumerate(values)]
        output = StringIO()
        csvwriter = csv.writer(output)
        csvwriter.writerow(cells)
        yield output.getvalue().rstrip('\r\n')


def display_rows(sheet, errors='strict'):
    csvrows = rows_as_csv(sheet, sys.stdout.encoding, errors)
    for line, row in enumerate(csvrows):
        # import pdb; pdb.set_trace()
        row = row.replace('\n', '\\n')
        echo(row, errors=errors, linenum=line)


@command()
@argument('workbook', type=Path(exists=True))
@argument('sheet', required=False, default=None)
@option('-l', '--list', 'list_', is_flag=True,
        help="List sheet numbers/names in WORKBOOK.")
@option('--strict', 'errors', flag_value='strict', default='strict',
        help="Exit program if a character can't be written to STDOUT.")
@option('--replace', 'errors', flag_value='replace',
        help="Replace a character with '?' if it can't be written to STDOUT.")
@option('--ignore', 'errors', flag_value='ignore',
        help="Ignore a character if it can't be written to STDOUT.")
def main(workbook, sheet, list_=False, errors=None):
    """Coverts an Excel workbook sheet to CSV format.

    SHEET can be a name or number. If the SHEET is not provided it
    lists available sheets an prompts for the sheet number.

    STDOUT friendly, escapes newlines found inside of cells.
    """
    book = open_book(workbook)
    if list_:
        list_sheets(book)
    else:
        if not sheet:
            sheet = prompt_for_sheetnum(book)
        funct = sheet_by_index if sheet.isdigit() else sheet_by_name
        sheet = funct(book, sheet)
        display_rows(sheet, errors)


if __name__ == '__main__':
    main()

以上是关于python 将Excel工作表转换为CSV。如果没有给出工作表名称或索引,将枚举可用工作表的列表并提示工作表麻木的主要内容,如果未能解决你的问题,请参考以下文章

将 Excel 工作簿中的所有工作表转换为 csv 格式

使用 Jupyter notebook 将具有多个工作表的 Excel 文件转换为多个 csv 文件

如何将单个工作表中的多行(在 excel 中)转换为多个 CSV 文件

Python编程快速上手——Excel到CSV的转换程序案例分析

如何在同一个Excel工作簿中将两个不同的CSV文件转换为两个工作表?

csv如何转换为excel?