How to sort data using XLSXWRITER in Python? - python

Currently I want to convert content in txt file to excel file using XLSXWRITER in python. The txt file contains some information with a specific datetime but it is not sorted from the latest date to oldest date. Is there any way to sort it and convert to excel file?

you can sort the data in your txt file based on the datetime and then convert it to an Excel file using XlsxWriter in Python. Here's an example code to do this:
import datetime
import xlsxwriter
# Read data from the txt file and sort it based on datetime
with open('data.txt', 'r') as f:
data = sorted(f.readlines(),
key=lambda x: datetime.datetime.strptime(x.split(',')[0],
'%Y-%m-%d %H:%M:%S'))
# Create a new Excel file and worksheet
workbook = xlsxwriter.Workbook('data.xlsx')
worksheet = workbook.add_worksheet()
# Write the sorted data to the worksheet
for row, line in enumerate(data):
values = line.strip().split(',')
for col, value in enumerate(values):
worksheet.write(row, col, value)
# Close the workbook
workbook.close()
In this code, we first read the data from the txt file and sort it based on datetime using the sorted() function and datetime.datetime.strptime() method. Then we create a new Excel file using XlsxWriter and add a new worksheet. Finally, we write the sorted data to the worksheet using a nested for loop and the worksheet.write() method.

Related

Merge two excel files together into one excel file with a sheet for each file while also retaining formatting

I have two excel files. One file is a table of data and the other file is a coversheet containing some dates/descriptive information.
Is there a way, preferably using openpyxl, where I can combine these two files (data and coversheet) into a new excel file (final) with one sheet containing the coversheet and a second sheet containing the data. I need to retain the formatting of the coversheet.
I can copy one file to another row by row
import openpyxl as op
from openpyxl import Workbook
dest_wb = Workbook()
dest_wb.create_sheet("Cover Sheet")
dest_ws = dest_wb["Cover Sheet"]
source_wb = op.load_workbook(r"C:\Users\coversheet.xlsx")
source_sheet = source_wb.active
for row in source_sheet.rows:
for cell in row:
dest_ws[cell.coordinate] = cell.value
dest_wb.save(r"C:\Users\test.xlsx")
dest_wb.close()
source_wb.close()
But this doesn't retain any of the formatting from the original file

How to import data from .txt file to a specifc excel sheet with Python?

I am trying to automate a process that basically reads in values from text files into certain excel cells. I have a template in excel that will read data from various sheets under certain names. For example, the template will read in data from "Video scores". Video scores is a .txt file that I copy and paste into excel. There are 5 different text files used in each project so it gets tedious after a while and when there are a lot of projects to complete.
How can I import or copy and paste these .txt files into excel to a specified sheet? I have been using openpyxl for the other parts of this project, but I am open to using another library if it can't be done with openpxl.
I've also tried opening and reading a file, but I couldn't figure out how to do what I want with that either. I have found a list of all the files I need, its just a matter of getting them into excel.
Thanks in advance for anyone who helps.
First, import the TXT file into a list in python, i'm asumming the TXT file is like this
1
2
3
4
....
with open(path_txt, "r") as e:
list1 = [i for i in e]
then, we paste the values of the list on the worksheet you need
from openpyxl import load_workbook
wb = load_workbook(path_xlsx)
ws = wb[sheet_name]
ws["A1"] = "values" #just a header
row = 2 #represent the 2 row of the sheet
column = 1 #represent the column "A" of the sheet
for i in list1:
ws.cell(row=row, column=column).value = i #getting the current cell, and writing the value of the list
row += 1 #just setting the current to the next
wb.save(path_xlsx)
Hope this works for you.
Pandas would do the trick!
Approach:
Have a sheet containing path to your files, separator, the corresponding target sheet names
Now read this excel sheet using pandas and iterate over each row for each file details, read the data, write it to new excel sheet of same workbook.
import pandas as pd
file_details_path = r"/Users/path for xl sheet/file details/File2XlDetails.xlsx"
target_sheet_path = r"/Users/path to target xl sheet/File samples/FiletoXl.xlsx"
# create a writer to save the file content in excel
writer = pd.ExcelWriter(target_sheet_path, engine='xlsxwriter')
file_details = pd.read_excel(file_details_path,
dtype = str,
index_col = False
)
def write_to_excel(file, trg_sheet_name):
# writes it to excel
file.to_excel(writer,
sheet_name = trg_sheet_name,
index = False,
)
# loop through each file record
for index, file_dtl in file_details.iterrows():
# you can print and check the row content for reference
print(file_dtl['File_path'])
print(file_dtl['Separator'])
print(file_dtl['Target_sheet_name'])
# reads file
file = pd.read_csv(file_dtl['File_path'],
sep = file_dtl['Separator'],
dtype = str,
index_col = False,
)
write_to_excel(file, file_dtl['Target_sheet_name'])
writer.save()
Hope this helps! Let me know if you run into any issues...

wrong output in converting date format data from an excel sheet to csv file in python

I have this excel sheet and I am trying to convert this excel sheet to csv file. Among the columns in this sheet is a column with data in date format(like 7/4/2017). I wrote this code but this is not converting the date field data correctly:
import xlrd
import csv
def Excel2CSV(ExcelFile, SheetName, CSVFile):
workbook = xlrd.open_workbook(ExcelFile)
worksheet = workbook.sheet_by_name(SheetName)
csvfile = open(CSVFile, 'w',encoding='utf8')
wr = csv.writer(csvfile,delimiter=';')
for rownum in range(worksheet.nrows):
wr.writerow(worksheet.row_values(rownum))
csvfile.close()
My sample data in excel is like this:
4/7/2017 value02 value03
4/5/2017 value12 value13
4/14/2017 value22 value23
4/10/2017 value32 value33
When I execute my above code this is what see in output:
42832.0;value02;value03
42830.0;value12;value13
42839.0;value22;value23
42835.0;value32;value33
As you can see that the date filed data is not getting converted correctly. What mistake I am making here?
Assuming you are using the XLRD package for reading the file you can find the answer at http://xlrd.readthedocs.io/en/latest/dates.html
Basically dates are stored as 'number of days since.....' and just formatted to appear as dates when viewed in Excel.
There are more details here
http://xlrd.readthedocs.io/en/latest/api.html#module-xlrd.xldate
'xldate_as_tuple' is the function you want

Two columns of data need to be saved as an excel file

I have two columns of data, x and y values, and need to save the file as an excel file to be opened in excel. Are there any modules that can help me with this?
The format needs to be xls
The data looks as follows:
4.20985 17.1047
4.82755 16.4046
3.17238 12.1246
4.50796 18.0955
6.04241 21.1016
4.62863 16.4974
4.32245 14.6536
6.48382 19.7664
5.66514 20.1288
6.11072 22.6859
5.55167 15.7504
It looks like that you'd be good with creating a csv file, but since you've asked about xls, here's an example using xlwt module:
import xlwt
data = """
4.20985 17.1047
4.82755 16.4046
3.17238 12.1246
4.50796 18.0955
6.04241 21.1016
4.62863 16.4974
4.32245 14.6536
6.48382 19.7664
5.66514 20.1288
6.11072 22.6859
5.55167 15.7504
"""
# prepare two-dimensional list
data = [map(float, item.split()) for item in data.split('\n') if item]
# create workbook and add sheet
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('Test')
# loop over two-dimensional list and write data
for index, (value1, value2) in enumerate(data):
sheet.write(index, 0, value1)
sheet.write(index, 1, value2)
# save a workbook
workbook.save('test.xls')
Hope that helps.

how to sort xls file column wise and write it to another file with entire row using python?

how to sort xls file column wise and write it to another file with entire row using python ? the xls file has to be sorted column wise. And after sorting it has to be writen into another file.
How about:
column = 0 #The column you want to sort by
reader = list(csv.reader(open('input.xsl')))
reader.sort(key=lambda x: x[column])
writer = csv.writer(open('output.xsl', 'w'))
writer.writerows(reader)
My bad, well you can always export as csv i guess. If you want to stick to xls you can use xlrd and xlwt. I haven't worked much with this but I do have a sample from a task I had to do a while back. Here it is(not that is not 100% good because the cell titles for each columns will be stored as the first row on data on the output file):
import xlwt
from xlrd import open_workbook
target_column = 0
book = open_workbook('input.xls', formatting_info=True)
sheet = book.sheets()[0]
data = [sheet.row_values(i) for i in xrange(sheet.nrows)]
labels = data[0]
data = data[1:]
data.sort(key=lambda x: x[target_column])
wbk = xlwt.Workbook()
sheet = wbk.add_sheet(sheet.name)
for idx, label in enumerate(labels):
sheet.write(0, idx, label)
for idx_r, row in enumerate(data):
for idx_c, value in enumerate(row):
sheet.write(idx_r+1, idx_c, value)
wbk.save('result.xls')

Categories