• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
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.  Using Python to Remove First Character from String
  • 2.  PROC FREQ Equivalent in Python
  • 3.  How to Group and Aggregate By Multiple Columns in Pandas
  • 4.  How to Find the Longest String in List in Python
  • 5.  Read Last N Lines of File in Python
  • 6.  Loop Through Files in Directory Using Python
  • 7.  Python Square Root Without Math Module – ** or Newton’s Method
  • 8.  Mastering Multiplication in Python: A Comprehensive Guide
  • 9.  Using Selenium to Close Browser in Python
  • 10.  How to Read Excel File from AWS S3 Bucket Using Python

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 *

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