Python (1) build directory and hyperlinks

# - * - Coding: UTF-. 8 - * - 
Import xlsxwriter   # import modules 
Import OS 

# Create txt file 
# F = Open ( 'PCL.bat', 'A') 
# f.write ( 'OFF @ echo') 
# f.write ( '\ ntree / F> Tree_result.txt') 
# f.write ( '\ Necho generated directory tree') 
# f.write ( '\ NDIR / S / B / P> directory.txt') 
# f.write ( '\ necho output file directory ...') 
# f.write ( '\ npython Dir.py') 
# f.write ( '\ necho build directory successfully!') 
# f.write ( '\ NPAUSE ') 
# f.close () 
# # execute the batch 
# os.system (r'PCL.BAT ') 

# definition file name
= txtName ' directory.txt ' 
txtname2 = ' Tree_result.txt ' 
excelname = ' directory navigation .xlsx ' 

# determines whether there is a temporary file in the current directory 
IF os.path.exists (txtName) == True: 
    The os.remove (txtName) 
IF os.path.exists (txtname2) == True: 
    os.remove (txtname2) 
# execute the batch command 
os.system ( ' @echo OFF ' ) 
os.system ( ' \ Necho generate a directory tree ... ' ) 
the os.system ( ' \ ntree / F >>   ' + txtname2)
the os.system ( ' \ Necho output Treeview success! ' ) 
the os.system ( ' \ Necho generate directory path absolute ... ' ) 
the os.system ( ' \ NDIR / S / B / P >> ' + txtName) 
os.system ( ' \ Necho output directory absolute path to success! ' ) 

# New EXCLE 
Workbook = xlsxwriter.Workbook (excelname)
 # New Sheet 
Worksheet = workbook.add_worksheet ( ' directory links ' ) 
worksheet2 = workbook.add_worksheet ( ' tree directory ' ) 

# import file data txt1 
fopen = open (txtname, ' R & lt' ) 
Lines = fopen.readlines () 

# import file data txt2 
fopen2 = Open (txtname2, ' R & lt ' ) 
lines2 = fopen2.readlines () 


# format directly by way of the dictionary. 
= workfomat1 workbook.add_format ({
     ' Bold ' : True,   # bold font 
    ' border ' :. 1,   # cell border width 
    ' align = left ' : ' Center ' ,   # alignment 
    ' valign ' : ' vCenter' ,   # Font Alignment 
    ' fg_color ' : ' # F4B084 ' ,   # cell background color 
    ' font_name ' : ' Microsoft yahei '   # Set Font 
}) 

workfomat2 = workbook.add_format ({
     ' Bold ' : False,   # font plus The crude 
    ' border ' :. 1,   # cell border width 
    " align = left ' : ' Center ' ,   # alignment 
    'font_name ' : ' Microsoft yahei ' ,   # Set Font 
    ' FONT_SIZE ' : 10 ,
     ' fg_color ' : ' # E6E4E1 ' ,   # cell background color 
}) 

workfomat3 = workbook.add_format ({
     ' Bold ' : False,   # font plus The crude 
    ' border ' :. 1,   # cell border width 
    ' align = left ' : ' left ' ,  # Alignment 
    'font_name' : ' Microsoft yahei ' ,   # Set Font 
    ' FONT_SIZE ' : 10 ,
     ' fg_color ' : ' # E6E4E1 ' ,   # cell background color 
}) 

workfomat4 = workbook.add_format ({
     ' Bold ' : False,   # bold font 
    ' align = left ' : ' left ' ,   # alignment 
    ' font_name ' : ' Times New Roman ' ,  # Set Font 
    ' FONT_SIZE ' :. 11 ,
     ' fg_color ' : ' # E6E4E1 ' ,   # cell background color 
}) 

Headings = [ ' number ' , ' link ' ]   # Set header 
worksheet.write_row ( ' A1 ' , Headings , workfomat1) 

# STR = '= the HYPERLINK (\ "D: \ Python 123.txt test \\ \")' 
# worksheet.write_formula (2,3, STR) 

I =. 1
 for Line in Lines: 
    the dir = line.strip ('\n')
    worksheet.write(i, 0, i, workfomat2)
    worksheet.write(i, 1, dir, workfomat3)
    worksheet.write(i, 1, '=HYPERLINK(\"' + dir + '\")')
    i = i + 1
worksheet.set_column('B:B', 150)

j = 0
for line2 in lines2:
    dir2 = line2.strip('\n')
    worksheet2.write (J, 0, dir2, workfomat4) 
    J = j + 1
worksheet2.set_column ( ' A: A ' , 300 ) 

# Close txt file 
fopen.close () 
fopen2.close () 
# close excle file 
workbook.close ()   # The save excel file is closed, if this line does not run error code 
# delete temporary files 
os.remove (txtName) 
os.remove (txtname2)

 

Guess you like

Origin www.cnblogs.com/aaronRhythm/p/11568476.html