在python中将标题写入excel文件

Sac*_*reD 2 python excel xlsxwriter

如何循环遍历列表中的每个元素并将其作为 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
Run Code Online (Sandbox Code Playgroud)

我想做一些像红色文字的事情 在此输入图像描述

Pet*_*ska 5

您可以使用 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
Run Code Online (Sandbox Code Playgroud)

DF 走向卓越

from pandas import ExcelWriter

writer = ExcelWriter('YourCSV.xlsx')
df.to_excel(writer,'Sheet1')
writer.save() #this create Excel file with your desired titles
Run Code Online (Sandbox Code Playgroud)

DF 转 CSV

df.to_csv('YourCSV.csv', sep=',')
Run Code Online (Sandbox Code Playgroud)