I am creating code for converting csv to xlsx but my code has a problem, it can convert the values only to A1 column. How to make code to be can writing values from csv to any selected column?
import csv
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
with open('text.csv') as f:
reader = csv.reader(f, delimiter=':')
for row in reader:
ws.append(row)
wb.save('file.xlsx')
CodePudding user response:
Why do you use open()
?
You can use pandas to open and save files:
pd.read_csv('text.csv').to_excel('file.xlsx')
CodePudding user response:
You need to use a comma (,
) not :
for delimiter.
I just tested this code and it made a perfect xlsx file with seperated columns.
import csv
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
with open('text.csv') as f:
reader = csv.reader(f, delimiter=',') # changed to ,
for row in reader:
ws.append(row)
wb.save('file.xlsx')
text.csv
a,b
1,2
3,4