1

I am writing a script to pull xml files from an FTP, turn them into an .xlsx file, and re-upload to a different directory on the same FTP. I want to create the .xlsx file within my script instead of copying the xml data into a template and uploading my local file.

I tried creating a filename for the .xlsx doc, but i realize that i need to save it before i can upload to the FTP. My question is, would it be better to create a temporary folder on the server the script is being run and empty the folder out afterwards? or is there a way to upload the doc without saving it anywhere (preferred)? I will be running the script on a windows server

ftps.cwd(ftpExcelDir)
wbFilename = str(orderID + '.xlsx')
savedFile = saving the file somwhere    # this is the part im having trouble with
ftps.storline('STOR ' + wbFilename, savedFile)

With the following code, i can get the .xlsx files to save to the FTP, but i recieve an invalid extension/corrupt file error from Excel:

ftps.cwd(ftpExcelDir)
wbFilename = str(orderID + '.xlsx')
inMemoryWB = io.BytesIO()
wb.save(inMemoryWB)
ftps.storbinary('STOR ' + wbFilename, inMemoryWB)
ajrice6713
  • 63
  • 1
  • 10
  • The preferred approach here will depend on OS. For example, on linux you can likely create a temporary file in `/dev/shm`, which will be easy to do and act like a normal file, but will actually only exist in memory. – James K Aug 28 '19 at 17:29
  • @JamesK - That would be a great fix, but i am running Windows unfortunatley – ajrice6713 Aug 28 '19 at 17:31
  • openpyxl requires file objects and you can't stream XLSX anyway. – Charlie Clark Aug 28 '19 at 19:58

1 Answers1

0

The ftp functions take file objects... but those don't strictly speaking need to be files. Python has BytesIO and StringIO objects which act like files, but are backed by memory. See: https://stackoverflow.com/a/44672691/8833934

James K
  • 597
  • 2
  • 11