Python enables Excel to be converted to JSON

Source: Internet
Author: User
python enables Excel to be converted to JSON First, preparePython operates on Excel by relying on the two modules of xlrd and XLWT.    XLRD is a module for reading Excel, XLWT for writing to Excel. Download: https://pypi.python.org/pypi/xlrd HTTPS://PYPI.PYTHON.ORG/PYPI/XLWT

If you just read Excel files, simply download xlrd
Second, code implementation:
#-*-Coding:utf-8-*-# @Date: 2016-07-13 14:47:56 # @Function: Convert Excel form to JSON file # @Author: Jennyzhang import XLR D Import JSON import codecs import OS #把excel表格中指定sheet转为json def excel2json (file_path): #打开excel文件 if Get_data (f
        Ile_path is not None:book = Get_data (file_path) #抓取所有sheet页的名称 worksheets = Book.sheet_names () Print "The list of forms that Excel contains is: \ n" for sheet in Worksheets:print ('%s,%s ') (Worksheets.index (sheet), she ET)) INP = raw_input (U ' please enter table single-name corresponding number, the corresponding form will automatically convert to json:\n ') sheet = book.sheet_by_index (int (INP)) row_0   = Sheet.row (0) #第一行是表单标题 nrows=sheet.nrows #行号 ncols=sheet.ncols #列号 result={}       #定义json对象 result["title"]=file_path #表单标题 result["Rows"]=nrows #行号 result["Children"]=[] #每一行作为数组的一项 #遍历所有行, convert Excel to a JSON object for I in range (nrows): If I==0:conti
 Nue tmp={}           #遍历当前行所有列 for J in Range (Ncols): #获取当前列中文标题 title_de=str (Row_0[j]). Decode (' Unicode_escape ') title_cn= title_de.split ("'") [1] #获取单元格的值 Tmp[tit Le_cn]=sheet.row_values (i) [j] result["Children"].append (TMP) json_data=json.dumps (result,indent= 4,sor t_keys=true). Decode (' Unicode_escape ') SaveFile (OS.GETCWD (), Worksheets[int (INP)],json_data) print Json_data # get Excel data Source def get_data (File_path): "" Get Excel Data Source "" Try:data = Xlrd.open_workbook (file_pat h) Return data except Exception, E:print U ' Excel table read failed:%s '%e return None def savefile (file_ Path,file_name,data): output = Codecs.open (file_path+ "/" +file_name+ ". Json", "W", "Utf-8") output.write (data) out
 Put.close () if __name__ = = ' __main__ ': File_path = raw_input (U ' Please enter Excel file path: \ n ') Json_data=excel2json (File_path)

Note: If the program fails in your environment, please check the code. Make a change in the place where you handle Chinese
Iii. Presentation of resultsRun the above code and follow the prompts. (1) Enter the path of the Excel file (as shown in the figure "e:/mid-exam.xlsx"), enter (2) Select the form number you want to convert (if 0 is selected in the figure), enter
(3) The output of the converted JSON. and save the results under the current path. The file name is table Single-name.



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.