Appending data when writing from excel sheet to txt document - excel

There is a text document called: file.txt with text already on it.
When I go through the excel sheet and write to the text file it erases the original text on it.
How do I only append the info from the excel sheet while keeping the original text info?
CODE
import xlwt
import xlrd
import csv
workbook = xlrd.open_workbook('input.xls')
sheet = workbook.sheet_by_index(2)
data = []
data.append([sheet.cell_value(row, 0).strip() for row in range(sheet.nrows)])
data.append([sheet.cell_value(row, 1).strip() for row in range(sheet.nrows)])
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('test')
for colidx, col in enumerate(data):
for rowidx, row in enumerate(col):
sheet.write(rowidx, colidx, row)
transposed = zip(*data)
with open('file.txt','wb') as fou:
writer = csv.writer(fou)
for row in transposed:
writer.writerow(row)

If I understand correctly you want to pass ab as a flag to open():
transposed = zip(*data)
# v
with open('file.txt','ab') as fou:
writer = csv.writer(fou)
for row in transposed:
writer.writerow(row)

Related

Python deleting row which not contains specific value?

If the ninth column doesn't contains the name of city ISTANBUL, then I need to be able to delete
the entire row. i wrote the code it works but it doesn't delete row.
from openpyxl import Workbook, load_workbook
from openpyxl.utils import get_column_letter
wb = load_workbook('deneme.xlsx')
ws = wb.active
for row in range(1, 3313):
for col in [9]:
char = get_column_letter(col)
if ws[char + str(row)].value is not 'İSTANBUL':
ws.delete_rows(row)
wb.save('deneme.xlsx')
Here it is solution i just found.
import pandas as pd
df = pd.read_excel('staji.xlsx') #here you can put your file path
filt = (df['ŞEHİR'] == 'İSTANBUL') # put your condition ŞEHİR=column name.
df[filt].to_excel('den1.xlsx')

Creating new sheet overwrites existing sheet created via openpyxl

I'm trying to create a bar chart directly in excel, using a pandas dataframe. In the same output excel, I'd like to save in a separate sheet the original csv used for the bar chart. My code:
wb = openpyxl.Workbook()
ws = wb.active
for row in dataframe_to_rows(new_df, index=False, header=False):
ws.append(row)
chart = BarChart()
values = Reference(ws, min_col=1, min_row=1, max_col=2, max_row=ws.max_row)
labels = Reference(ws, min_col=1, min_row=1, max_col=1, max_row=ws.max_row)
chart.add_data(values)
chart.set_categories(labels)
ws.add_chart(chart, "E2")
wb.save("~/barChart.xlsx")
writer = pd.ExcelWriter("~/barChart.xlsx", engine='openpyxl')
df.to_excel(writer, sheet_name="Source_data")
writer.save()
The problem I get is the the last three lines, which overwrite the produced bar chart. How do I overcome this?
from pandas documentation:
ExcelWriter can also be used to append to an existing Excel file:
with pd.ExcelWriter('output.xlsx', mode='a') as writer:
df.to_excel(writer, sheet_name='Sheet_name_3')

Python for the Comparison of excel column elements and print the matched elements in separate column

I have developed the following code and fetched the matched output using a for loop.I need to print these output elements in separate column using python.
excel file name - Sample_data.xlsx
first column - WBS_CODE
second column - PROJECT_CODE
first column and second column are matched and then printed in separate column (column F) using python code. Please find my below code,
import pandas as pd
A = pd.read_excel("D:\python_work\Sample_data.xlsx", sheet_name = '31Sep')
code = A['WBS_CODE'].tolist()
B = pd.read_excel("D:\python_work\Sample_data.xlsx", sheet_name = '4Dec')
code1 = B['PROJECT_CODE'].tolist()
for x in code1:
if x in code:
print(x)
else:
print("NA")
output:
NA
NA
NA
APP-ACI-PJ-APAC-EMEA-ENG
NA
NA
I have found a way to export the output and print them in a separate column in excel sheet. Below is the solution,
import pandas as pd
from openpyxl import load_workbook
# Reading the Excel file columns
A = pd.read_excel("D:\python_work\Sample_data.xlsx", sheet_name='4Dec')
code = A['PROJECT_CODE'].tolist()
B = pd.read_excel("D:\python_work\Sample_data.xlsx", sheet_name='31Sep')
code1 = B['WBS_CODE'].tolist()
# Comparison of columns
class test:
def loop(self):
result = []
for x in code1:
if x in code:
result.append(x)
else:
y = "NA"
result.append(y)
print(result)
# Printing data into Excel
try:
book = load_workbook('D:\python_work\Aew1.xlsx')
writer = pd.ExcelWriter('D:\python_work\Aew1.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets) # loading all the worksheets in opened Excel
df = pd.DataFrame.from_dict({'Column1': result})
df.to_excel(writer, sheet_name='Sheet1', startcol=19)
writer.save()
except FileNotFoundError:
print("File Not found: 'Check the Name or Existence of file specified/'")
except PermissionError:
print("File Opened/No-Access: Check whether you have access to file or file is opened")
test().loop()
steps that solved:
1. Appended the for loop output to a list
2. used openpyxl library to print the output to a column in excel worksheet.
Thanks guyz for help and support. Have a good day

How to read an excel file with multiple sheets using for loop in python

This is what i try
from pathlib import Path
loc = Path('D:\DataSciSpec\Practice\Forloopindict.xlsx')
dict = pd.read_excel(loc,sheetname = None)
for i in dict.keys():
print(i)
I get the name of sheets
Sheet4
Sheet3
Sheet2
Sheet1
I can also see the sheet content one by one
for i in dict.keys():
print(dict[i].head())
But how put this data in n data frames (equal to no of sheets)
and then append one to another
This will create a single dataframe (df_full) with the data from all sheets.
import pandas as pd
loc = r'D:\DataSciSpec\Practice\Forloopindict.xlsx'
workbook = pd.read_excel(loc,sheet_name = None)
df_full = pd.DataFrame()
for _, sheet in workbook.items():
df_full = df_full.append(sheet)
# Reset index or you'll have duplicates
df_full = df_full.reset_index(drop=True)

excel automation using python

I am trying to write a function to read data from excel file using python. My function should read rows from excel sheet one at a time. Below is my code which will print 1st row.
import xlrd
from xlrd import open_workbook, cellname
book = open_workbook('./Excel/Book1.xls')
def read_excel(sheetName):
sheet = book.sheet_by_name(sheetName)
row = sheet.nrows
for i in range(1):
rows = sheet.row_values(i+1)
print(rows)
file = r'd:\pythonTest.xlsx '
import xlrd
wb = xlrd.open_workbook(file)
sheet = wb.sheet_by_index(0)
# For row 0 and column 0
sheet.cell_value(0, 0)
# Extracting number of columns
print(sheet.ncols)
print(sheet.nrows)
print(sheet.row_values(1))
for i in range(sheet.nrows):
print(sheet.cell_value(i, 0), sheet.cell_value(i, 1))

Resources