pu369com

python(openpyxl)复制excel数据到另一个excel数据表

之前写过https://www.cnblogs.com/pu369/p/15430224.html

代码:

#-*- coding: utf-8 -*-
from openpyxl import Workbook
from openpyxl import load_workbook
import openpyxl

#global
items = []
file1 = r"F:\统计表.xlsx"
#新建excel
wb2=openpyxl.Workbook()
wb2.save('test.xlsx')
print('新建成功')

#读取数据
wb1=openpyxl.load_workbook(file1)
wb2=openpyxl.load_workbook('test.xlsx')
#获取sheet页
sheet1=wb1._sheets[0]
sheet2=wb2._sheets[0]

for m in range(1,100):
    v = sheet1["H"+str(m)].value
    if v == None:       
        pass
    else:
        w =v.split("\n")
        for x in w:
            items.append(x)
for n in range(1,len(items)):    
    sheet2["A"+str(n)].value=items[n]
        
# 调整列宽
sheet2.column_dimensions['A'].width = 200.0
#单元格自动换行
for r in sheet2:
    for c in r:
        c.alignment = openpyxl.styles.Alignment(wrapText=True)

wb2.save('test.xlsx')#保存数据
wb1.close()#关闭excel
wb2.close()

 

posted on 2021-10-23 18:03  pu369com  阅读(2177)  评论(0编辑  收藏  举报

导航