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