Py学习  »  Python

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

RisoGalloExresso • 3 年前 • 2386 次点击  

在我的代码下面:

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
 
2386 次点击  
文章 [ 2 ]  |  最新文章 3 年前