Connecting from Python to Excel 2016(365) and perform tasks - python

I have a problem connecting to Excel API in windows 10. I use Office365 and with it Excel2016. My goal is: to download CSV file from a client FTPS Server, merge it with the existing files,perfom some action on it(with pandas) and then load the whole data into excel and do reporting with it... Up to the point of loading it into Excel everything is fine.I managed to do all steps automatically with Python (sorry if my code looks a little cluttered - I am new to Python)
import subprocess
import os
import ftplib
import fnmatch
import sys
from ftplib import FTP_TLS
from win32com.client import Dispatch
import pandas as pd
filematch = '*.csv'
target_dir = 'cannot tell you the path :-) '
def loginftps(servername,user,passwort):
ftps = FTP_TLS(servername)
ftps.login(user=user,passwd=passwort)
ftps.prot_p()
ftps.cwd('/changes to some directory')
for filename in ftps.nlst(filematch):
target_file_name = os.path.join(target_dir,os.path.basename(filename))
with open(target_file_name,'wb') as fhandle:
ftps.retrbinary('RETR %s' %filename, fhandle.write)
def openExcelApplication():
xl = Dispatch("Excel.Application")
xl.Visible = True # otherwise excel is hidden
def mergeallFilestoOneFile():
subprocess.call(['prepareData_executable.bat'])
def deletezerorows():
rohdaten = pd.read_csv("merged.csv",engine="python",index_col=False,encoding='Latin-1',delimiter=";", quoting = 3)
rohdaten = rohdaten.convert_objects(convert_numeric=True)
rohdaten = rohdaten[rohdaten.UN_PY > 0]
del rohdaten['deletes something']
del rohdaten['deletes something']
rohdaten.to_csv('merged_angepasst.csv',index=False,sep=";")
def rohdatenExcelAuswertung():
csvdaten = pd.csv_read("merged.csv")
servername = input("please enter FTPS serveradress:")
user = input("Loginname:")
passwort = input("Password:")
loginftps(servername,user,passwort)
mergeallFilestoOneFile()
deletezerorows()
And here I am stuck somehow,.. I did extensive google research but somehow nobody has ever tried to perform Excel tasks from within Python??
I found this stackoverflow discussion: Opening/running Excel file from python but I somehow cannot figure out where my Excel-Application is stored to run code mentioned in this thread.
What I have is an Excel-Workbook which has a data connection to my CSV file. I want Python to open MS-Excel, refresh data connection and refresh a PivoTable & then save and close the file.
Has anybody here ever tried to to something similar and can provide some code to get me started?
Thanks

A small snippet of code that should work for opening an excel file, updating linked data, saving it, and finally closing it:
from win32com.client import Dispatch
xl = Dispatch("Excel.Application")
xl.Workbooks.Open(Filename='C:\\Users\\Xukrao\\Desktop\\workbook.xlsx', UpdateLinks=3)
xl.ActiveWorkbook.Close(SaveChanges=True)
xl.Quit()

Related

How to open password protected Excel file?

So I have this code that I got from here:
How to open a password protected excel file using python?
from xlrd import *
import win32com.client
import csv
import sys
xlApp = win32com.client.Dispatch('Excel.Application')
print('Excel Library Version:', xlApp.Version)
xlwb = xlApp.Workbooks.Open(r'Y:\Production\Production Planning\Production Thruput\% of Completion.xlsm', False, True, None, 'pass2016')
However, it's not working for me. I do not get an error, and the screen even shows the downloading dialog box (screenshot shown below) like if it's about to open my file... and then nothing. What am I doing wrong? I've been researching this for 2 hours and can't seem to find any answers that work for me

access remote files on server with smb protocol python3

I have a remote server with some files.
smb://ftpsrv/public/
I can be authorized there as an anonymous user. In java I could simply write this code:
SmbFile root = new SmbFile(SMB_ROOT);
And get the ability to work with files inside (it is all I need, one row!), but I can't find how to manage with this task in Python 3, there are a lot of resources, but I think they are not relevant to my problem, because they are frequently tailored for Python 2, and old other approaches. Is there some simple way, similar to Java code above?
Or can somebody provide a real working solution if, for example, I want to access file fgg.txt in smb://ftpsrv/public/ folder. Is there really a handy lib to tackle this problem?
For example on site:
import tempfile
from smb.SMBConnection import SMBConnection
# There will be some mechanism to capture userID, password, client_machine_name, server_name and server_ip
# client_machine_name can be an arbitary ASCII string
# server_name should match the remote machine name, or else the connection will be rejected
conn = SMBConnection(userID, password, client_machine_name, server_name, use_ntlm_v2 = True)
assert conn.connect(server_ip, 139)
file_obj = tempfile.NamedTemporaryFile()
file_attributes, filesize = conn.retrieveFile('smbtest', '/rfc1001.txt', file_obj)
# Retrieved file contents are inside file_obj
# Do what you need with the file_obj and then close it
# Note that the file obj is positioned at the end-of-file,
# so you might need to perform a file_obj.seek() if you need
# to read from the beginning
file_obj.close()
Do I seriously need to provide all of these details: conn = SMBConnection(userID, password, client_machine_name, server_name, use_ntlm_v2 = True)?
A simple example of opening a file using urllib and pysmb in Python 3
import urllib
from smb.SMBHandler import SMBHandler
opener = urllib.request.build_opener(SMBHandler)
fh = opener.open('smb://host/share/file.txt')
data = fh.read()
fh.close()
I haven't got an anonymous SMB share ready to test it with, but this code should work.
urllib2 is the python 2 package, in python 3 it was renamed to just urllib and some stuff got moved around.
I think you were asking for Linux, but for completeness I'll share how it works on Windows.
On Windows, it seems that Samba access is supported out of the box with Python's standard library functions:
import glob, os
with open(r'\\USER1-PC\Users\Public\test.txt', 'w') as f:
f.write('hello') # write a file on a distant Samba share
for f in glob.glob(r'\\USER1-PC\Users\**\*', recursive=True):
print(f) # glob works too
if os.path.isfile(f):
print(os.path.getmtime(f)) # we can get filesystem information

Accessing UpdateLinks() in COM Object using Python

I am working on automating an Excel file which is linked to certain .csv files.
Those .csv files are created from a SAS Code which is run every Quarter.
The files created are timestamped accordingly for example XYZ_201603.csv and XYZ_201606.csv and so on.
I need to update the links on my Excel File so that it automatically changes the link to the file from next quarter. I am trying to do this using Python win32com.client and my code looks like
from win32com import Dispatch
xl_app = Dispatch("Excel.Application")
xl_app.Visible = True
xl_app.DisplayAlerts = False
wb = xl_app.workbooks.open(r"C:\Users\XYZ\Desktop\Test\Summary.xlsx")
xl_app.AskToUpdateLinks = False
try:
wb.UpdateLink(Name=r"C:\Users\XYZ\Desktop\Test\XYZ_201606.csv")
except Exception as e:
print(e)
finally:
wb.Close(True)
wb = None
return True
xl_app.Quit()
xl = None
Whenever I run this, I get the following error
(-2147352567,'Exception occured.',(0,'Microsoft Excel','UpdateLink method of
Workbook class failed','xlmain11.chm',0,-2146827284),None)
Can Somebody tell me what is going wrong here. Also, incase I have multiple links, how do I tell which link needs to be changed to what? Can I pass a dictionary of directories of updated datasets
The code and the approach has been taken from this answer on Stack Overflow
Update Links in for Excel Spreadsheet Using Python
If you review the Microsoft Documentation, it seems that the UpdateLink method can be called without any parameters. Therefore this program should work:
import win32com.client as win32
xl_app = win32.gencache.EnsureDispatch("Excel.Application")
xl_app.Visible = True
xl_app.DisplayAlerts = False
wb = xl_app.workbooks.open(r"C:\Users\XYZ\Desktop\Test\Summary.xlsx")
wb.UpdateLink()
wb.Save()
wb.Close()
xl_app.Quit()
I'm not sure if my solution solves your issue, but I had the same problem and I used LinkSources() and ChangeLink() instead
newSource = r"C:\Users\XYZ\Desktop\Test\XYZ_201606.csv"
oldSource = wb.LinkSources()
wb.ChangeLink(Name = oldSource[0], NewName = newSource, Type = 1)
Hope it helps!

unotools - try to convert ods or excel files to csv using python

What I need is a command line tool to convert excel and ods spreadsheet files to csv which I can use on a web server (Ubuntu 16.04).
I already red this: https://pypi.python.org/pypi/unotools
which works fine for the given examples.
And this: http://www.linuxjournal.com/content/convert-spreadsheets-csv-files-python-and-pyuno-part-1v2
which should do the work I want it to do, but does not in my environment.
My problem I think is in the method Calc.store_to_url:
Line throwing exception
component.store_to_url(url,'FilterName','Text - txt - csv (StarCalc)')
I really would appreciate a hint.
Exception
unotools.unohelper.ErrorCodeIOException: SfxBaseModel::impl_store failed: 0x81a
Full source
import sys
from os.path import basename, join as pathjoin, splitext
from unotools import Socket, connect
from unotools.component.calc import Calc
from unotools.unohelper import convert_path_to_url
from unotools import parse_argument
def get_component(args, context):
_, ext = splitext(args.file_)
url = convert_path_to_url(args.file_)
component = Calc(context, url)
return component
def convert_csv(args, context):
component = get_component(args, context)
url = 'out/result.csv'
component.store_to_url(url,'FilterName','Text - txt - csv (StarCalc)')
component.close(True)
args = parse_argument(sys.argv[1:])
context = connect(Socket(args.host, args.port), option=args.option)
convert_csv(args, context)
The URL must be in file:// format.
url = convert_path_to_url('out/result.csv')
See the store_to_url example at https://pypi.python.org/pypi/unotools.
EDIT:
To use the absolute path, choose one of these; there is no need to combine them.
url = 'file:///home/me/out/result.csv'
url = convert_path_to_url('/home/me/out/result.csv')
To use the relative path, first verify that the working directory is '/home/me' by calling os.getcwd().

how to output xlsx generated by Openpyxl to browser?

I was using stackoverflow for a while now and it helped me very often. Now I have a problem I couldn't solve myself or through searching.
I'm trying to output my excel file generated by openpyxl in browser as I was doing it with phpexcel. The method appears to be the same, but I only get broken file. My code looks like this:
from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter
from openpyxl.writer.excel import save_virtual_workbook
from openpyxl.cell import get_column_letter
from StringIO import StringIO
print 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
print 'Content-Disposition: attachment;filename="results.xlsx"'
print 'Cache-Control: max-age=0\n'
output = StringIO()
wb = Workbook()
ws = wb.worksheets[0]
ws.cell('A1').value = 3.14
wb.save(output)
print output.getvalue()
#print save_virtual_workbook(wb)
I use the version 1.5.8 and python 2.7.
None of the approaches works. When I just use it from desktop and not browser it works flawlessly.
I would be very thankful for help.
P.S. please don't tell me that using other language or program would be easier. I need to solve this with python.
this is work for me. I use python 2.7 and latest openpyxl and send_file from flask
... code ...
import StringIO
from openpyxl import Workbook
wb = Workbook()
ws = wb.active # worksheet
ws.title = "Excel Using Openpyxl"
c = ws.cell(row=5, column=5)
c.value = "Hi on 5,5"
out = StringIO.StringIO()
wb.save(out)
out.seek(0)
return send_file(out, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
attachment_filename='xxl.xlsx', as_attachment=True)
output = HttpResponse(mimetype='application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
file_name = "Test.xlsx"
output['Content-Disposition'] = 'attachment; filename='+ file_name
wb = Workbook()
ws = wb.worksheets[0]
ws.cell('A1').value = 3.14
wb.save(output)
return output
I used this tips to download my files with openpyxl. Hope that will help
Writing the xlsx output to disk and then serving it up via Apache worked perfectly, but putting it out directly caused errors in Excel and other issues.
I added a couple of extra steps and made one minor change to your code:
buffer=output.getvalue()
In the HTTP headers:
print "Content-Length: " + str(len(buffer))
And used write() instead of print() to push the buffer into the standard output stream:
stdout.write(buffer)
Your scripts works for me as you expect without alterations.
I can only assume you have a problem with your cgi script setup.
Make sure you have the directory where the script lives actually gets served by the web server. On apache you can achieve this with:
ScriptAlias /cgi-bin/ /home/WWW/localhost/cgi-bin/
Make sure the script is excutable by setting the script permissions. For commandline operation (python scriptname) that was not necessary, for your webbrowser that is. And make sure the owner of the webserver can excute the scripts, as the webserver probably does not run as you.
Because Excel uses a binary format you should be using BytesIO to buffer.
from io import BytesIO
But what error are you getting if you use save_virtual_workbook() which does this for you?
I have same problem.
Solution is to switch stdout to bin mode:
import msvcrt
print 'Content-Type:application/octet-stream; name="{}"'.format(os.path.basename(xls_file))
print 'Content-Disposition:attachment; filename="{}"'.format(os.path.basename(xls_file))
print "Content-Length: " + str(os.path.getsize(xls_file))
print 'Cache-Control: max-age=0\r\n'
msvcrt.setmode (1, os.O_BINARY) # stdout = 1
sys.stdout.flush()
with open(xls_file, 'rb') as fobj:
copyfileobj(fobj, sys.stdout)
If you want to build an HTML table that looks like your spreadsheet, you probably want to work with CSV. Either do this, instead of Excel, OR convert your Excel to CSV after you build it.
In any case, once you have the data in CSV format, then it's simply a matter of using python to build the HTML page and looping through the CSV data, while inserting your <table>, <tr>, and <td> tags, as appropriate.

Categories