Reputation: 2454
So basically I got an xlsm document, it contains a sheet "data" and a graph, the graph is generated from data.
I have 5 CSV files.
I need to erase the content of data, then fill it with the 5 CSV files.
Is it possible to do without putting my csv file in an array and writing them line per line (very time consuming).
Can I just open my CSV files and sort of paste them into the data sheet?
Thanks
Upvotes: 0
Views: 1383
Reputation: 20341
You can just open CSV files in Excel, and you can join your CSV files into one larger one with
my_files = ['file_1.csv', 'file_2.csv', 'file_3.csv', 'file_4.csv', 'file_5.csv']
with open('output.csv', 'w') as oo:
for file in my_files:
with open(file, 'r') as io:
oo.write(io.readlines())
you can then open output.csv
in excel.
If you don't want to manually list all your input files, you could use glob
>>> my_files = glob.glob('file_*.csv')
>>> my_files
... ['file_1.csv', 'file_2.csv', 'file_3.csv', 'file_4.csv', 'file_5.csv']
There is even an Excel read / write python module here. You could use it to write directly to an Excel file:
import xlwt
w = xlwt.Workbook()
ws = w.add_sheet('First sheet')
i = 0
for file in my_files:
with open(file, 'r') as fo:
for line in fo.readlines():
for j, col in enumerate(line.split(',')):
ws.write(i, j, col)
i += 1
Upvotes: 2