1

I'm going to poll controllers in our data centers and output all of them to a csv file. The python tool, xlsxwriter, looks to be the best for it. However, I don't see any mention of how to simply take a csv file and convert it to xlsx.

Xlsxwriter seems to be great for making an xlsx file based on the python script it's in, but I don't know how to gather that data from a csv file.

clickwir
  • 11
  • 1
  • 2
  • What about importing the csv-file into Excel using Excel's import function? I don't think you need to do that Python-based since I do not see any advantages by doing so. The only usecase I can think about might be something like combining several csv-files to one xlsx-file. _What_ have you tried so far? – albert Feb 16 '16 at 17:37
  • Save yourself some time and have the user open using Excel. What you're attempting to do is not fun... Some references if you wanna deal with it http://stackoverflow.com/questions/17684610/python-convert-csv-to-xlsx – FirebladeDan Feb 16 '16 at 17:46
  • I'm trying to gather stats from a few hundred devices and have each device be it's own line in a spreadsheet. Then when we are in meetings and someone says "Hey, how many of these do we have running? How many of those are running X.Y.Z code level? How long have they been running that?" we can just look at the spreadsheet and sort it and get the answers. – clickwir Feb 17 '16 at 17:05

1 Answers1

4

If you don't mind an answer with another package dependency, I highly recommend pandas for I/O operations like this. It's hard to beat in terms of both code economy and performance. Also, if you need to do any manipulations (filtering, sorting, etc.) on the data before writing to xslx, it's already in a handy dataframe.

You could do something like:

import pandas as pd
import xlsxwriter

path = 'some/path/'
#read the csv into a pandas dataframe
data = pd.read_csv(path + 'input.csv')    
#setup the writer
writer = pd.ExcelWriter(path + 'output.xlsx', engine='xlsxwriter')
#write the dataframe to an xlsx file
data.to_excel(writer, sheet_name='mysheet', index=False)
writer.save()
America
  • 408
  • 2
  • 5
  • 16