社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python和openpyxl-在新文件中“追加”一行时,如何复制样式?

RisoGalloExresso • 3 年前 • 2392 次点击  

在我的代码下面:

import openpyxl

# file 1:
path = "C:\\Users\\Admin\\Desktop\\TEST.xlsx"
wb_1 = openpyxl.load_workbook(path, read_only=True)
ws_1 = wb_1[wb_1.sheetnames[0]]

# file 2 (it's a new file):
wb_2 = openpyxl.Workbook()
ws_2 = wb_2.active
ws_2.title = "SHEET"

# copy the rows placed in file 1 into the file 2:
def GetList(row):
    list_for_row_values = []
    for cell in row:
        list_for_row_values.append(cell.value)
    return list_for_row_values

for row in ws_1.rows:
    list_for_row_values = GetList(row)
    ws_2.append(list_for_row_values)

wb_2.save("C:\\Users\\Admin\\Desktop\\result.xlsx")

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/99356
 
2392 次点击  
文章 [ 2 ]  |  最新文章 3 年前