Create a folder tree structure from Excel using a Python script

Source: Internet
Author: User
Tags python script

Maybe it's right to write the title ...

Goal:

Generates a folder tree structure from an Excel directory structure document.

That is

Through the following Excel

  

Generate the following document tree structure:

  

Method:

1, Analysis: The general document structure is a good idea beforehand. You can make a draft in the TXT document and paste it into Excel. As follows:

  

  one thing to note here : when pasting from the TXT document, the Excel document, the default is only one column in Excel if the TXT rating is a space. If the TXT rating is tab, then each column is in Excel. Of course, you can set it yourself.

2, the Excel document structure of the road to complement the whole.

  

3. Write the py script:

#Coding:utf-8ImportxlrdImportOSImportSysdirpath= Os.path.dirname (Os.path.realpath (__file__)) Excelfile= ur"%s"%sys.argv[1]#transcoding Chinese characters to avoid garbled charactersEncodetext =LambdaA:a.encode ("gb2312")defGet_data_by_xls (xlsfile):" "get a list of data with XLS documents" "WB=Xlrd.open_workbook (xlsfile) Table=wb.sheets () [0] rows=table.nrows forIinchRange (table.nrows):yieldtable.row_values (i)defCreatedir (dirpath,xlist):" "apply recursion, generate document number" "    Try: RPath=Os.path.join (Dirpath,encodetext (xlist[0]))Try: Os.mkdir (rPath) rList= Xlist[1:]        except: RList= Xlist[1:] Createdir (rpath,rlist)except:        Pass     forIinchGet_data_by_xls (excelfile):ifI:createdir (dirpath,i)Print "Create dir complete!"

4. Operation:

  

Get structure:

  

5. Summary:

(1) When the document is generated, the Chinese should be transcoded, otherwise it will cause garbled characters.

(2) The path is recursively processed when the folder is generated.

(3) in the Excel document directory, each directory should be complete for the full path.

(4) When getting a list of directories, the script uses the yield generator to avoid the problem that the Excel catalog consumes too much memory, but general Excel does not. Returning directly to the list is OK.

==============================================================================

Life is short, I use python!

Create a folder tree structure from Excel using a Python script

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.