zoukankan      html  css  js  c++  java
  • Python(1)生成目录及超链接

    # -*- coding: utf-8 -*-
    import xlsxwriter  # 导入模块
    import os
    
    # 新建txt文档
    # f = open('PCL.bat','a')
    # f.write('@echo off')
    # f.write('
    tree /f > Tree_result.txt')
    # f.write('
    echo 生成树形目录')
    # f.write('
    dir /s/b/p > directory.txt')
    # f.write('
    echo 输出文件目录...')
    # f.write('
    python Dir.py')
    # f.write('
    echo 生成目录成功!')
    # f.write('
    pause')
    # f.close()
    # #执行批处理
    # os.system(r'PCL.bat')
    
    #定义文件名称
    txtname = 'directory.txt'
    txtname2 = 'Tree_result.txt'
    excelname = '目录导航.xlsx'
    
    #判断当前目录下是否存在临时文件
    if os.path.exists(txtname)==True:
        os.remove(txtname)
    if os.path.exists(txtname2)==True:
        os.remove(txtname2)
    #执行批处理命令
    #os.system('@echo off')
    #os.system('
    echo 生成树形目录...')
    os.system('
    tree /f >>  ' + txtname2)
    #os.system('
    echo 输出树形目录成功!')
    #os.system('
    echo 生成目录绝对路径...')
    os.system('
    dir /s/b/p >> ' + txtname)
    #os.system('
    echo 输出目录绝对路径成功!')
    
    # 新建excle
    workbook = xlsxwriter.Workbook(excelname)
    # 新建sheet
    worksheet = workbook.add_worksheet('目录链接')
    worksheet2 = workbook.add_worksheet('树形目录')
    
    # 导入txt1文件数据
    fopen = open(txtname, 'r', encoding='gbk')
    lines = fopen.readlines()
    
    # 导入txt2文件数据
    fopen2 = open(txtname2, 'r', encoding='gbk')
    lines2 = fopen2.readlines()
    
    
    # 通过字典的方式直接设置格式。
    workfomat1 = workbook.add_format({
        'bold': True,  # 字体加粗
        'border': 1,  # 单元格边框宽度
        'align': 'center',  # 对齐方式
        'valign': 'vcenter',  # 字体对齐方式
        'fg_color': '#F4B084',  # 单元格背景颜色
        'font_name': '微软雅黑'  # 设置字体
    })
    
    workfomat2 = workbook.add_format({
        'bold': False,  # 字体加粗
        'border': 1,  # 单元格边框宽度
        'align': 'center',  # 对齐方式
        'font_name': '微软雅黑',  # 设置字体
        'font_size': 10,
        'fg_color': '#E6E4E1',  # 单元格背景颜色
    })
    
    workfomat3 = workbook.add_format({
        'bold': False,  # 字体加粗
        'border': 1,  # 单元格边框宽度
        'align': 'left',  # 对齐方式
        'font_name': '微软雅黑',  # 设置字体
        'font_size': 10,
        'fg_color': '#E6E4E1',  # 单元格背景颜色
    })
    
    workfomat4 = workbook.add_format({
        'bold': False,  # 字体加粗
        'align': 'left',  # 对齐方式
        'font_name': '宋体',  # 设置字体
        'font_size': 11,
        'fg_color': '#E6E4E1',  # 单元格背景颜色
    })
    
    headings = ['序号', '链接']  # 设置表头
    worksheet.write_row('A1', headings, workfomat1)
    
    
    i = 1
    for line in lines:
        dir = line.strip('
    ')
        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('
    ')
        worksheet2.write(j, 0, dir2, workfomat4)
        j = j + 1
    worksheet2.set_column('A:A', 300)
    
    #关闭txt文件
    fopen.close()
    fopen2.close()
    #关闭excle文件
    workbook.close()  #将excel文件保存关闭,如果没有这一行运行代码会报错
    #删除临时文件
    os.remove(txtname)
    os.remove(txtname2)
  • 相关阅读:
    350. Intersection of Two Arrays II
    94. Binary Tree Inorder Traversal
    623. Add One Row to Tree
    JS判断是否为数字,中文,小写、大写字母
    ASP.NET 操作Cookie详解 增加,修改,删除
    ASP.NET MVC 入门1、简介
    通过LINQ TO SQL类显示数据库表的数据
    OutputCache缓存优化asp.net代码 提高网页性能
    数据库读取二进制图片显示到PictureBox中
    WinForm窗体间如何传值的几种方法
  • 原文地址:https://www.cnblogs.com/aaronRhythm/p/11568476.html
Copyright © 2011-2022 走看看