python操作拷贝excel到另外一个excel中

import xlrd
import xlwt

# 需求:拷贝excel中的内容到另外一个excel中

flile_name = "7月下旬入库表.xlsx"
# 读取源excel
xlsx = xlrd.open_workbook(flile_name)

# xlsx = xlrd.open_workbook("readexcel.xlsx")
# 获取sheet个数
sheets = len(xlsx.sheets())

# 准备写入
new_workbook = xlwt.Workbook()

for sheet in range(sheets):
    table = xlsx.sheet_by_index(sheet)
    rows = table.nrows
    cols = table.ncols
    worksheet = new_workbook.add_sheet("sheet"+str(sheet))
    for i in range(0,rows):
        for j in range(0, cols):
            # print(i,j,table.cell_value(i, j))
            worksheet.write(i, j ,table.cell_value(i, j))
new_workbook.save("copy" + flile_name)

将文件列表写入excel中
import os
import xlwt

file_dir = "d:/"
listdirs = os.listdir(file_dir)
workbook = xlwt.Workbook()
sheet0 = workbook.add_sheet("newtest")
n = 0
for i in listdirs:
    sheet0.write(n,0,i)
    n += 1
workbook.save("listdirs.xls")

 

 
posted @ 2020-03-13 23:47  reblue520  阅读(6365)  评论(0编辑  收藏  举报