0
votes

I am trying to create a script that takes user input and outputs an excel file with the user input as the columns headings. So if the user inputs: Col1,Col2,Col3 .. the output should be an excel file with 3 columns each name respectively to: Col1,Col2,Col3.

Code:

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)      

Assuming I run the script with an input of : Col1,Col2,Col3,Col4,Col5 The current output for the script is: Col5 Col5 Col5 Col5 Col5
Current output

The Result I am looking for is: Col1 Col2 Col3 Col4 Col5

Expected output

Does anyone know what is wrong with the current script? Why is that the created excel file has the 5 columns all named Col5 and not Col1 Col2 Col3 Col4 Col5 Thanks!

1
What is the real question: you have a list column_names holding 5 values: column_names = ['Col1', 'Col2', 'Col3', 'Col4', 'Col5'] (you have to check it). Now you want to write the values in the right order to cells A1, B1, C1, D1, E1. Can't be too difficult, can it?Elmex80s
The script write the values in the right order to cells but it doesn't write the right value. It should write 'Col1', 'Col2', 'Col3', 'Col4', 'Col5' respectively to columns A1, B1, C1, D1, E1.. instead it write Col5 Col5 Col5 Col5 Col5 respectivelyto A1, B1, C1, D1, E1David
That is what I said.Elmex80s

1 Answers

1
votes

try zip at the end:

# Write some data headers.
for i, name in zip(t, column_names):
    worksheet.write(i,name,bold)