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")
 
        

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM