使用python导出文件夹内文件名excel(机械零件BOM单)

# coding=utf-8
import os
import xlwt  # 操作excel模块
import sys

file_path = sys.path[0] + '\钣金加工询价清单.xls'  # sys.path[0]为要获取当前路径,filenamelist为要写入的文件
f = xlwt.Workbook(encoding='utf-8', style_compression=0)  # 新建一个excel
sheet = f.add_sheet('sheet1')  # 新建一个sheet
pathDir = os.listdir(sys.path[0])  # 文件放置在当前文件夹中,用来获取当前文件夹内所有文件目录
sheet.write(0, 0, "钣金加工询价清单")  # 参数i,0,s分别代表行,列,写入值
sheet.write(1, 0, "序号")  # 
sheet.write(1, 1, "零件名称")  # 
sheet.write(1, 2, "材料")  # 
sheet.write(1, 3, "数量(件)")  # 
sheet.write(1, 4, "单价")  # 
sheet.write(1, 5, "总价")  # 


i = 2  # 将文件列表写入test.xls
for s in pathDir:
  if  s.endswith("pdf") or s.endswith("PDF") :
    if s[0]=="T":
      sheet.write(i, 1, s[:-4])  # 参数i,0,s分别代表行,列,写入值
      sheet.write(i, 0, i-1)  # 参数i,0,s分别代表行,列,写入值
      i = i + 1

print(file_path)
print(i)  # 显示文件名数量
f.save(file_path)

  

原文地址:https://www.cnblogs.com/robohou/p/11699501.html