xlsxwriter:根据用户输入和 for 循环创建标题
xlsxwriter: create headings from user input and for loops
我正在尝试创建一个脚本,该脚本接受用户输入并输出一个 excel 文件,其中用户输入作为列标题。
因此,如果用户输入:Col1,Col2,Col3 .. 输出应该是一个 excel 文件,每个名称有 3 列,分别为:Col1,Col2,Col3.
代码:
import xlsxwriter
from itertools import islice
column_names = input()
template_name = input()
# add file type to template name
file_type = '.xlsx'
template_name = template_name + file_type
# create a list of the input names
column_names = column_names.split(",")
# xlsxwriter column position logic based on user input length
alpha = list(map(chr, range(65, 91)))
z =[]
for letter in alpha:
z.append(letter + str(1))
t =[]
for i in islice( z, 0, len(column_names) ):
t.append(i)
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook(template_name )
worksheet = workbook.add_worksheet()
# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
# Write some data headers.
for name in column_names:
for i in t:
worksheet.write(i,name,bold)
假设我 运行 脚本的输入为:Col1,Col2,Col3,Col4,Col5
该脚本的当前输出是:Col5 Col5 Col5 Col5 Col5
Current output
我要找的结果是:Col1 Col2 Col3 Col4 Col5
Expected output
有谁知道当前脚本有什么问题吗?为什么创建的 excel 文件有 5 列都命名为 Col5 而不是 Col1 Col2 Col3 Col4 Col5
谢谢!
最后尝试压缩:
# Write some data headers.
for i, name in zip(t, column_names):
worksheet.write(i,name,bold)
我正在尝试创建一个脚本,该脚本接受用户输入并输出一个 excel 文件,其中用户输入作为列标题。 因此,如果用户输入:Col1,Col2,Col3 .. 输出应该是一个 excel 文件,每个名称有 3 列,分别为:Col1,Col2,Col3.
代码:
import xlsxwriter
from itertools import islice
column_names = input()
template_name = input()
# add file type to template name
file_type = '.xlsx'
template_name = template_name + file_type
# create a list of the input names
column_names = column_names.split(",")
# xlsxwriter column position logic based on user input length
alpha = list(map(chr, range(65, 91)))
z =[]
for letter in alpha:
z.append(letter + str(1))
t =[]
for i in islice( z, 0, len(column_names) ):
t.append(i)
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook(template_name )
worksheet = workbook.add_worksheet()
# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
# Write some data headers.
for name in column_names:
for i in t:
worksheet.write(i,name,bold)
假设我 运行 脚本的输入为:Col1,Col2,Col3,Col4,Col5
该脚本的当前输出是:Col5 Col5 Col5 Col5 Col5
Current output
我要找的结果是:Col1 Col2 Col3 Col4 Col5
Expected output
有谁知道当前脚本有什么问题吗?为什么创建的 excel 文件有 5 列都命名为 Col5 而不是 Col1 Col2 Col3 Col4 Col5 谢谢!
最后尝试压缩:
# Write some data headers.
for i, name in zip(t, column_names):
worksheet.write(i,name,bold)