Python Forum
Filter Excel and Convert an Excel File
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filter Excel and Convert an Excel File
#1
Hello everyone,

The following is my attempt to filter the last modified xls file on a ftp server and convert that file to a csv on the same server.

from datetime import datetime

import ftplib

import ftputil

import pandas as pd

# Download some files from the login directory.

with ftputil.FTPHost('ftp.123.com', 'username', 'password') as host:

flz= []

name2 = host.curdir

names = host.listdir(host.curdir)

print(host.curdir)

print(name2)

for name in names:

if host.path.splitext(name)[1] == '.xls':

mtime = host.path.getmtime(name)

converttime = datetime.fromtimestamp(mtime).strftime('%Y-%m-%d %H:%M:%S')

flz.append(name)

print(name,converttime)

for i in range(0, len(flz)):

if i == (len(flz) - 1):

print("This is the last element : " + str(flz[i]))

# Read the file

if host.path.isfile(str(flz[i])):

# Read the file

read_file = pd.read_excel(host.getcwd()(f'{flz[i]}'))

# Reconstruct file name, converting extension from '.xls' to '.csv'

output_csv = f"{host.getcwd()(str(flz[i]).name.replace('.xls', ''))}.csv"

# Create output csv file

read_file.to_csv(output_csv, index=None, header=True)

# Read csv file into DataFrame

df = pd.DataFrame(pd.read_csv(f"{host.curdir(get.cwd() / 'output_csv')}"))

# Output DataFrame

print(df)

print('Done')
When ran I get an error message of:
Traceback (most recent call last):
  File "C:\Users\mrdrj\Desktop\Desk--2021 April 23\Desktop(Update)\ftpdownload.py", line 29, in <module>
    read_file = pd.read_excel(host.getcwd()(f'{flz[i]}'))
TypeError: 'str' object is not callable
How can I solve the filter and convert to csv ftp issue? Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Automated filler for an Excel form is not writing the data Quian34 2 36 Jun-10-2026, 07:27 AM
Last Post: Larz60+
  Python and excel vba coding odiez 2 2,903 Sep-12-2025, 11:09 AM
Last Post: DeaD_EyE
  How to export files from spyder to excel? kranklin 3 1,966 Aug-04-2025, 08:51 PM
Last Post: kranklin
  Excel password recovery tool for work OTH 6 8,191 Mar-06-2025, 03:49 PM
Last Post: Pedroski55
  python read PDF Statement and write it into excel mg24 1 1,787 Sep-22-2024, 11:42 AM
Last Post: Pedroski55
  docx file to pandas dataframe/excel iitip92 1 5,689 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  Excel isnt working properly after python function is started IchNar 2 1,968 May-01-2024, 06:43 PM
Last Post: IchNar
  Updating formulas in Excel - best solution MasterOfDestr 4 7,231 Mar-11-2024, 01:02 PM
Last Post: MasterOfDestr
  Python openyxl not updating Excel file MrBean12 1 3,950 Mar-03-2024, 12:16 AM
Last Post: MrBean12
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 16,800 Feb-29-2024, 12:30 AM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020