一、xlsxwriter的使用:
# import xlwt
#
# workbook = xlwt.Workbook()
# sheet0 = workbook.add_sheet('sheet0')
# for i in range(0,300):
# sheet0.write(0,i,i)
# workbook.save('number.xls')# 以上代码运行会报错,因为xlwt不支持超过256列的表格import xlsxwriter as xw
workbook = xw.Workbook('./number.xlsx')
sheet0 = workbook.add_worksheet('sheet0')
for i in range(0,300):sheet0.write(0,i,i)
workbook.close()
追加写入库openpyxl 的使用:
import openpyxl# 打开工作本
workbook = openpyxl.load_workbook('number.xlsx')# 打开工作本中的工作表
sheet0 = workbook['sheet0']# 向单元格写入值
sheet0['B3'] = '5'
sheet0['B4'] = 6# 保存工作本
workbook.save('测试.xlsx')
三、案例:
读取指定路径下的文件目录,并把文件名存到一个Excel表格中:
import os
import xlwtfile_dir = r'D:\BStudy\大二下\网课软件指南'# 列出该路径下的文件目录
os.listdir(file_dir)
print(os.listdir(file_dir))new_workbook = xlwt.Workbook()
worksheet = new_workbook.add_sheet('new_test')n = 0
for i in os.listdir(file_dir):worksheet.write(n,0,i)n += 1new_workbook.save('file_name.xls')
运行结果: