• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Output XLSX File from Pandas to Remote Server Using Paramiko FTP

How to Output XLSX File from Pandas to Remote Server Using Paramiko FTP

March 10, 2021 2 Comments

Many times, when working with files and remote servers, it would be helpful if we could use code to manipulate directories and files to make processes more efficient.

In many organizations, Microsoft Excel files store data for different processes, and from time to time, we need to update the data stored in these files.

Having to update these files manually can be a nightmare. Adding to that, if you are working with remote servers, there will most likely need to be some copying back and forth to your local computer.

With Python, we can write a program that does these manipulations for us, and save a lot of headache.

Using Paramiko and Pandas, we can easily write xlsx files to a remote server.

How to Output an XLSX File to Remote Server Using Pandas, Paramiko and FTP

Below is the code that I use to output xlsx files to a remote server using Pandas and Paramiko.

First, we connect to the server. We can read from existing Excel files, or create our own. Then, we write back the Dataframe contents to the remote server in a XLSX file.

import io
import paramiko
import pandas as pd

#connect to remote server

host = "yourhost"
username = "yourusername"
password = "yourpassword"

con = paramiko.SSHClient()
con.load_system_host_keys()
ftp.set_missing_host_key_policy(paramiko.AutoAddPolicy())
con.connect(host, username, password)
ftp = con.open_sftp()

#read in existing xlsx file contents to dataframe

existing_xlsx = ftp.open("yourfilepath/existingfilename.xlsx")
df = pd.read_excel(existing_xlsx)

#xlsx manipulations here

#output xlsx file back to remote server
output = io.BytesIO()
writer = pd.ExcelWriter(output, engine='openpyxl')
df.to_excel(writer, sheet_name="Sheet1", index=False)
writer.save()
xlsx_data = output.getvalue()
with ftp.open('yourfilepath/yourfilename.xlsx', "w", bufsize=32768) as f: 
    f.write(xlsx_data)

Hopefully, this post has helped you with automating a process using Python and manipulating Microsoft Excel files on your remote server.

Other Articles You'll Also Like:

  • 1.  Break Out of Function in Python with return Statement
  • 2.  Remove Time from Datetime in Python
  • 3.  Python Infinity – How to Use Infinity in Python
  • 4.  Get Current Year in Python
  • 5.  pandas Drop Rows – Delete Rows from DataFrame with drop()
  • 6.  Sort Series in pandas with sort_values() Function
  • 7.  pandas ewm – Calculate Exponentially Weighted Statistics in DataFrame
  • 8.  How to Serialize a Model Object with a List of Lists Using Django Rest Framework in Python
  • 9.  Cartesian Product of Two Lists in Python
  • 10.  Get Days in Month from Date in pandas DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Comments

  1. JS says

    August 13, 2021 at 8:20 am

    Can you do the opposite? Bringing an xlsx from a remote server using the same packages?

    Reply
    • Erik says

      November 23, 2021 at 8:30 am

      Yes, this is quite easy as it is just using the same Paramiko and then reading it in – instead of writing it out.

      Here’s our post to help you do this: http://theprogrammingexpert.com/read-xlsx-file-from-remote-server-using-paramiko-ftp-pandas/

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy