Convert the python script to xls (xlsx) to csv and pythonxlsx

Source: Internet
Author: User

Convert the python script to xls (xlsx) to csv and pythonxlsx

# Xls_csv

Convert xls and xlsx files to csv files.

# Use
Python xls2csv. py <xls or xlsx file path>

# -*- coding: utf-8 -*-import xlrdimport xlwtimport sysfrom datetime import date,datetime def read_excel(filename):   workbook = xlrd.open_workbook(filename)  # print sheet2.name,sheet2.nrows,sheet2.ncols  sheet2 = workbook.sheet_by_index(0)    for row in xrange(0, sheet2.nrows):    rows = sheet2.row_values(row)    def _tostr(cell):      if type(u'') == type(cell):         return "\"%s\"" % cell.encode('utf8')      else:        return "\"%s\"" % str(cell)       print ','.join([_tostr(cell) for cell in rows ])  if __name__ == '__main__':  filename = sys.argv[1]  read_excel(filename)

I will share with you a piece of code.

Xlsx file parsing: csv file format generated by openpyxl Library: csv

Python # coding: UTF-8 # dependent openpyxl Library: http://openpyxl.readthedocs.org/en/latest/from openpyxl import Workbookfrom openpyxl. compat import rangefrom openpyxl. cell import get_column_letterfrom openpyxl import load_workbookimport csvimport osimport sysdef xlsx2csv (filename): try: xlsx_file_reader = load_workbook (filename = filename) for sheet in workflow (): # Each sheet is output to a csv file. Use '_' to connect the x file name to the sheet name to csv_filename = '{xlsx}_{sheet}.csv '. format (xlsx = OS. path. splitext (filename. replace ('', '_') [0], sheet = sheet. replace ('', '_') csv_file = file (csv_filename, 'wb ') csv_file_writer = csv. writer (csv_file) sheet_ranges = xlsx_file_reader [sheet] for row in sheet_ranges.rows: row_container = [] for cell in row: if type (cell. value) = unicode: row_container.append (cell. value. encode ('ut F-8 ') else: row_container.append (str (cell. value) csv_file_writer.writerow (row_container) csv_file.close () except t Exception as e: print (e) if _ name _ = '_ main _': if len (sys. argv )! = 2: print ('usage: xlsx2csv <xlsx file name> ') else: xlsx2csv (sys. argv [1]) sys. exit (0)

Articles you may be interested in:
  • Example of reading csv files using python (using csv in python)
  • How python processes csv data
  • Python calls the java module SmartXLS and jpype to modify the excel file
  • How to export data from Python to CSV files that can be read in Excel
  • How to Write a CSV file using Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.