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)
Related
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)
here is my code:
import openpyxl as op
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl import worksheet
path = ...
op.load_workbook(path)
wb = Workbook()
ws2 = wb.create_sheet('Sheet2')
wb.save(filename = 'NameOfTheFile.xlsx')
Now everything works, except saving the file, when I add:
print(wb.sheetnames)
it does print out the Sheet2 I just added, but for some reason when I go to the documents where the file is, it does not update it, that sheet is nowhere to be found.
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()
I am trying following code.
from zipfile import ZipFile
from openpyxl import load_workbook
from io import BytesIO
zip_path = r"path/to/zipfile.zip"
with ZipFile(zip_path) as myzip:
with myzip.open(myzip.namelist()[0]) as myfile:
wb = load_workbook(filename=BytesIO(myfile.read()))
data_sheet = wb.worksheets[1]
for row in data_sheet.iter_rows(min_row=3, min_col=3):
print(row[0].value)
it shows
ValueError: stat: path too long for Windows
Is this possible?
I am trying logic from Using openpyxl to read file from memory
With xlrd following code works fine.
with ZipFile(zip_path) as myzip:
with myzip.open(myzip.namelist()[0]) as myfile:
book = xlrd.open_workbook(file_contents=(myfile.read()))
sh = book.sheet_by_index(0)
#your code here
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)