加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python(1)生成目录及超链接

发布时间:2020-12-20 10:37:00 所属栏目:Python 来源:网络整理
导读:# -*- coding: utf-8 -*- import xlsxwriter # 导入模块 import os # 新建txt文档 # f = open(‘PCL.bat‘,‘a‘) # f.write(‘@echo off‘) # f.write(‘ntree /f Tree_result.txt‘) # f.write(‘necho 生成树形目录‘) # f.write(‘ndir /s/b/p direct
# -*- coding: utf-8 -*-
import xlsxwriter  # 导入模块
import os

# 新建txt文档
# f = open(‘PCL.bat‘,‘a‘)
# f.write(‘@echo off‘)
# f.write(‘ntree /f > Tree_result.txt‘)
# f.write(‘necho 生成树形目录‘)
# f.write(‘ndir /s/b/p > directory.txt‘)
# f.write(‘necho 输出文件目录...‘)
# f.write(‘npython Dir.py‘)
# f.write(‘necho 生成目录成功!‘)
# f.write(‘npause‘)
# 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(necho 生成树形目录...)
os.system(ntree /f >>   + txtname2)
os.system(necho 输出树形目录成功!)
os.system(necho 生成目录绝对路径...)
os.system(ndir /s/b/p >>  + txtname)
os.system(necho 输出目录绝对路径成功!)

# 新建excle
workbook = xlsxwriter.Workbook(excelname)
# 新建sheet
worksheet = workbook.add_worksheet(目录链接)
worksheet2 = workbook.add_worksheet(树形目录)

# 导入txt1文件数据
fopen = open(txtname,r)
lines = fopen.readlines()

# 导入txt2文件数据
fopen2 = open(txtname2,r)
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,# 对齐方式
    font_name: 微软雅黑,# 设置字体
    font_size: 10,fg_color: #E6E4E1,# 单元格背景颜色
})

workfomat3 = workbook.add_format({
    bold: False,# 单元格边框宽度
    align: left,# 单元格背景颜色
})

workfomat4 = workbook.add_format({
    bold: False,# 字体加粗
    align: left,# 对齐方式
    font_name: 宋体,# 设置字体
    font_size: 11,# 单元格背景颜色
})

headings = [序号,链接]  # 设置表头
worksheet.write_row(A1,headings,workfomat1)

# str = ‘=HYPERLINK("D:python测试123.txt")‘
# worksheet.write_formula(2,3,str)

i = 1
for line in lines:
    dir = line.strip(n)
    worksheet.write(i,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,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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读