Read and manipulate xlsx file from local storage and extract some of the values from cells into .txt and .csv files

432 Views Asked by At

Read excel file and extract the details into a new .txt file using openpyxl

I am new to openpyxl, just started yesterday. I need to extract the data from the excel file from my local storage that excel file have 500+ rows and 50+ columns. I want to extract some specific cells or columns or rows into the .txt and .csv file.

I cannot find where is the mistake what to add in this code

from openpyxl import *
import os    

path = 'F:\\mis'
files = [i for i in os.listdir(path) if i.endswith('.xlsx')]

for f in files:
     wb = load_workbook(os.path.join(path, f))
     for row in wb['newxl.xlsx'].rows:
         with open(row[2].value+'.txt', 'w') as outfile:
              outfile.write(row[0].value)```
2

There are 2 best solutions below

15
Robin Sage On BEST ANSWER

UPDATED ANSWER

import openpyxl
import csv
      
roster = openpyxl.load_workbook('roster.xlsx')
sheet = roster.active
col = csv.writer(open("new_roster.csv",'w',newline=""))

st_id = int(input("Enter student ID: "))
for row in sheet.rows:
    if row[0].value == st_id:
        col.writerow([cell.value for cell in row])
print("File created!")

UPDATE 2

This is how you can get the values from a specific column:

import openpyxl
roster = openpyxl.load_workbook('roster.xlsx')  
sheet = roster.active
col = sheet['B']  

for x in range(len(col)): 
    print(col[x].value) 

UPDATE 3

Return specific values from column:

import openpyxl

roster = openpyxl.load_workbook('roster.xlsx')  
sheet = roster.active

col = sheet['B']  # <-- change the column according to your file
val = input("Enter value: ")
for c in range(len(col)): 
    if val == col[c].value:
        print(f'Found {col[c].value}!')
0
AudioBubble On
import pandas as pd
df=pd.read_excel("F:\\mis\\CASHE.xlsx")
cus_id=input("Please enter your customer id:")
cashe=['customer_id', 'customer_name', 'login_email_id', 'login_source', 'New_date', 'customer_status_name']

if cus_id == '':
    df1=df[cashe]
else:
    df1=df[cashe].loc[df['customer_id']==int(cus_id)]

df1.to_csv('Cashe1.csv')
print("csv printed with values")