Skip to main content
 首页 » 编程设计

python之在python中将标题写入excel文件

2025年05月04日13del

如何遍历列表中的每个元素并将其作为 excel 标题?让我知道是否有重复的问题。到目前为止我找不到它。

row=0 
col=0 
 
j = 0 
title = ['No.', 'Hue', 'Saturation', 'Value', 
         'Lightness', 'AComponent', 'BComponent', 
         'Blue Channel', 'Green Channel', 'Red Channel'] 
 
for i in title[0:len(title)]: 
    worksheet.write(row + 1, col + j, 'title[%i]', bold) 
    j += 1 

我想做一些像红色的文字

请您参考如下方法:

您可以使用 Pandas 和 ExcelWriter 模块

使用列表中的列(即您案例中的标题)创建空 DF

import pandas as pd 
 
title = ['No.', 'Hue', 'Saturation', 'Value', 
         'Lightness', 'AComponent', 'BComponent', 
         'Blue Channel', 'Green Channel', 'Red Channel'] 
 
df = pd.DataFrame(columns = title) #this create your empty data frame 

追求卓越

from pandas import ExcelWriter 
 
writer = ExcelWriter('YourCSV.xlsx') 
df.to_excel(writer,'Sheet1') 
writer.save() #this create Excel file with your desired titles 

DF 到 CSV

df.to_csv('YourCSV.csv', sep=',')