how to convert from CVS to existing excel file? - python

I am trying to convert a CSV file to an existing excel file named 'bench_configuration'.Inside this Excel there are few sheets. I want to convert the CSV file into a sheet named 'Setup_Loss' (which is already inside the EXCEL file)
I tried to use :
read_file = pd.read_csv('new_names.csv',sep='\t')
read_file.to_excel('bench_configuration.xlsx', index=None, header=True)
But its opening a new Excel file.

You need to have an instance of ExcelWriter and use it to write and save the content
import pandas as pd
from openpyxl import load_workbook as lw
workbook = lw('bench_configuration.xlsx')
writer = pd.ExcelWriter('bench_configuration.xlsx', engine='openpyxl', mode='a')
writer.book = workbook
writer.sheets = dict((ws.title, ws) for ws in workbook.worksheets)
read_file = pd.read_csv('new_names.csv',sep='\t')
read_file.to_excel(writer, "Setup_Loss")
writer.save()

Related

How to copy data from txt file and paste to XLSX as value with Python?

How to copy data from txt file and paste to XLSX as value with Python?
(txt)File: simple.txt which contains date,name,qty,order id
I need the data from txt and copy paste to xlsx as VALUE.
How it's possible it? Which package could handle this process with Python?
openpyxl?Panda? Could you please give an example code?
My code which not suitable for the paste and save as values:
import csv
import openpyxl
input_file = 'C:\Users\mike\Documents\rep\LX02.txt'
output_file = 'C:\Users\mike\Documents\rep\LX02.xlsx'
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
with open(input_file, 'r') as data:
reader = csv.reader(data, delimiter='\t')
for row in reader:
ws.append(row)
wb.save(output_file)
In pandas, with pandas.read_csv and pandas.DataFrame.to_excel combined, you can store the content of a comma delimited .txt file in an .xlsx spreedsheet by running the code below :
#pip install pandas
import pandas as pd
input_file = r'C:\Users\mbalog\Documents\FGI\LX02.txt'
output_file = r'C:\Users\mbalog\Documents\FGI\LX02.xlsx'
pd.read_csv(input_file).to_excel(output_file, index=False)

Moving data from multiple csv files to xlsx files

I have a folder that contains 2 more folders. Inside each folder is a csv and xlsx file.
Ex:
test (folder 1)
test.csv
test.xlsx
test2 (folder 2)
test2.csv
test2.xlsx
I have a working script that moves data from a csv file to a xlsx file.
Say ‘test.csv’ contains the following data:
A
B
test.com
yes
test.com/dl
no
1.1.1.1
yes
The code below will move that data into test.xlsx:
from openpyxl import load_workbook
import csv
wb = load_workbook(“D:\\local\\test\\test\\test.xlsx”)
ws = wb.active
with open(“D:\\local\\test\\test\\test.csv”, ‘r’) as f:
for row in csv.reader(f):
ws.append(row)
wb.save(“D:\\local\\test\\test\\test.xlsx”)
Is there an easy way to move all data from ‘test.csv’ to ‘test.xlsx’ and ‘test2.csv’ to ‘test2.xlsx’ at once? The names of the csv and xlsx files will not always be the same but the location will.
I have tried the following but it returns a traceback error:
from openpyxl import load_workbook
import csv
wb = load_workbook(“D:\\local\\test\\{}\\{}.xlsx”)
ws = wb.active
with open(“D:\\local\\test\\{}\\{}.csv”, ‘r’) as f:
for row in csv.reader(f):
ws.append(row)
wb.save(“D:\\local\\test\\{}\\{}.xlsx”)
Thanks!
Assuming that the .xlsx files already exist and are empty, you can use the code below to copy the content of multiple .csv files to those .xlsx files (that have the same stem/filename).
import os
from pathlib import Path
import pandas as pd
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
directory = r'D:\local\test'
for file in Path(directory).glob('*/*.csv'):
df = pd.read_csv(file, encoding='utf-8-sig')
excel_path = os.path.splitext(file)[0]+'.xlsx'
wb = load_workbook(excel_path)
ws = wb.active
for r in dataframe_to_rows(df, index=False, header=True):
ws.append(r)
wb.save(excel_path)

python - pandas with openpyxl damages my workbook

Using pandas with the openpyxl engine I want to open an excel woorkbook and add a sheet.
Because, the sheets are growing and getting quit big I want to create a new sheet without reading all the other sheet.
So instead of xlsxwriter, I did start using openpyxl, however I get the following issue that the file gets damaged and needs to recovered by excel.
After that, when I run it a second time, I get the python error that the excel file is 'raise BadZipFile("File is not a zip file")'.
This my test code:
import openpyxl
import pandas
from pandas import ExcelWriter
from pandas import ExcelFile
from openpyxl import Workbook
from openpyxl import load_workbook
sheet1 = 'sheet1'
sheet2 = 'sheet2'
if os.path.exists(filename):
workbook = openpyxl.load_workbook(filename)
else:
workbook = Workbook()
writer = pandas.ExcelWriter(filename, engine='openpyxl')
writer.book = workbook
df_1.to_excel(writer, sheet_name=sheet1)
writer.save()
writer.close()
time.sleep(2) # to give it some time. But doesn't help :(
if os.path.exists(filename):
workbook = openpyxl.load_workbook(filename)
else:
workbook = Workbook()
writer = pandas.ExcelWriter(filename, engine='openpyxl')
writer.book = workbook
df_2.to_excel(writer, sheet_name=sheet2)
writer.save()
writer.close()
Any suggestions how to solve this? Or do I miss something?
btw, excel 365 - 16.46 macOS

Excel file can not be opened anymore after writing with pd.ExcelWriter

I am writing a dataframe to a range of Excel file in a certain tab, but after saving the file, I see that the Excel file has become unusable. Could anyone suggest a solution?
import openpyxl as pyx
df3_xmax= df3.iloc[0]
wb = pyx.load_workbook(dst)
xl_writer = pd.ExcelWriter(dst, engine='openpyxl')
xl_writer.book = wb
xl_writer.sheets = {ws.title:ws for ws in wb.worksheets}
df3_xmax.to_excel(xl_writer, 'shname', index=False, header=False, startcol=3, startrow=7)
xl_writer.save()

Converting .txt file with tab seperation to xlsx via python3

Level: super-noob
I have been trying to convert a .txt file to .xlsx using a combination of csv & openpyxl & xlsxwriter modules.
My first column is an identity that should be saved as a string
Columns 2-21 are then all numbers.
How can I load up my .txt file.
Identify the proper columns as numbers
and then save the file as an xlsx?
So far I'm at:
import csv
import openpyxl
input_file = "C:/1.txt"
output_file = "C:/1.xlsx"
new_wb = openpyxl.Workbook()
ws = new_wb.worksheets[0]
read_file = csv.reader(input_file, delimitter="\t")
I have read people using enumerate to gun through an excel file online but I'm not sure how this function exactly works... but if someone can help me here it will be appreciated!
You need to iterate over each row in csv file and append that row to excel worksheet.
This could be helpful:
import csv
import openpyxl
input_file = 'path/to/inputfile.txt'
output_file = 'path/to/outputfile.xls'
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
with open(input_file, 'rb') as data:
reader = csv.reader(data, delimiter='\t')
for row in reader:
ws.append(row)
wb.save(output_file)

Categories