How to download multiple worksheets using Smartsheet API? - python

I want to script the download of 8 sheets using the module smartsheet-python-sdk.
Later I want to automate the download scheduled at a specific time once a week with Windows Task Scheduler.
But I have trouble with the Python script downloading the sheets via API.
This is what I have in a .py file.
import os, smartsheet
token=os.environ['SMARTSHEET_ACCESS_TOKEN']
smartsheet_client = smartsheet.Smartsheet(token)
smartsheet_client.errors_as_exceptions(True)
smartsheet_client.Reports.get_report_as_excel(
8729488427892475,
'C:/Desktop',
'MyFileName.xlsx'
)

I notice a couple of issues with the code you've posted.
First, smartsheet_client.Reports.get_report_as_excel should be smartsheet_client.Sheets.get_sheet_as_excel if you're wanting to retrieve a sheet (not a report).
Next, try specifying the full path for where Desktop actually lives on the file system. i.e., instead of just using the alias C:\Desktop -- specify the actual file system path, for example: c:/users/USERNAME/desktop (on a Windows machine) where USERNAME is the name of the currently logged in user. You can also derive the logged-in user dynamically, see How to make Python get the username in windows and then implement it in a script.
The following code successfully downloads the specified sheet to my Desktop, with the filename MyFileName.xlsx. (I'm logged in as kbrandl so the path to my Desktop is specified as: c:/users/kbrandl/desktop.)
sheetId = 3932034054809476
smartsheet_client.Sheets.get_sheet_as_excel(
sheetId,
'c:/users/kbrandl/desktop',
'MyFileName.xlsx'
)

Related

Using selenium in python to download files based on file names from Ambari (HDFS)

I want to be able to download all csv files in a specified path within Ambari file viewer that have a certain file name.
i.e. Open ambari file viewer on google chrome, log into ambari file viewer with username and password, navigate to a specified folder in ambari, download all relevant csv files based on file name using wildcards (e.g. file__20191231.csv), place files in a specified windows folder
Seems very doable, I'm not exactly sure what your question is though and I'm not familiar with Ambari. To tackle a project like this I suggest the following steps:
Step 1: Research Selenium and practice things like logging into a social media or another web account.
Step 2: Specifically look at the section for identifying items by id, class, and xpath. Check the html of Ambari and see if the id's or classes seem reliable for the elements you need to interact with (ie. username & password fields).Use xpath if you must.
Step 3: Find the column/container that the files are displayed in and create a loop to pull the text out of each container. Add page pagination if needed.
Step 4: use python to read each text as it is viewed, if it contains the substring you desire, ask selenium to right click the element it just visited and download (or click the corresponding download button if available).
Step 5: Move the file from your downloads folder to your desired folder on your machine with os and shutil there's another thread about doing this here
P.S. You'll need a compatable chromedriver.exe to run selenium with chrome. Again, view the selenium docs to learn more about python-selenium and setting it up.
Hope this helps

How to access Excel data which is in Github from AWS machines by using Python

I have an excel file placed in Github and Python installed in AWS machine. I wanted to read the excel file from the AWS machine using Python script. Can you some one help me how to achieve this. So far i used below code to achieve this...
#Importing required Libraries
import pandas as pd
import xlwt
import xlrd
#Formatting WLM data
URL= 'https://github.dev.global.tesco.org/DotcomPerformanceTeam/Sample-WLM/blob/master/LEGO_LIVE_FreshOrderStableProfile_2019_v0.1.xlsx'
data = pd.read_excel(r"URl", sheet_name='WLM', dtype=object)
When i executed this i am getting below error
IOError: [Errno 2] No such file or directory: 'URl'
You can use de Wget command to download the file from GitHub. The key here is to use the raw version link, otherwise you will download an html file. To get the raw link, click on the file you uploaded on GitHub, then right-click on the Raw button and choose the save path or copy path. Finally you can use it to download the file, and then read it with pd.read_excel("Your Excel file URL or disk location"). Example:
#Raw link: https://raw.github.com/<username>/<repo>/<branch>/Excelfile.xlsx
!wget --show-progress --continue -O /content/Excelfile.xlsx https://raw.github.com/<username>/<repo>/<branch>/Excelfile.xlsx
df = pd.read_excel("content/Excelfile.xlsx")
Note: this example applies for Colab if you are using a local environment do not use the exclamation mark. You can also find more ideas here: Download single files from GitHub
These instruction are for a CSV file but should work for an excel file as well.
If the repository is private, you might need to create a personal access token as described in "Creating a personal access token" (pay attention to the permissions especially if the repository belongs to an organisation).
Click the "raw" button in GitHub. Here below is an example from https://github.com/udacity/machine-learning/blob/master/projects/boston_housing/housing.csv:
If the repo is private and there is no ?token=XXXX at the end of the url (see below), you might need to create a personal access token and add it at the end of the url. I can see from your URL that you need to configure your access token to work with SAML SSO, please read About identity and access management with SAML single sign-on and Authorizing a personal access token for use with SAML single sign-on
Copy the link to the file from the browser navigation bar, e.g.:
https://raw.githubusercontent.com/udacity/machine-learning/master/projects/boston_housing/housing.csv
Then use code:
import pandas as pd
url = (
"https://raw.githubusercontent.com/udacity/machine-learning/master"
"/projects/boston_housing/housing.csv"
)
df = pd.read_csv(url)
In case your repo is private, the link copied would have a token at the end:
https://raw.githubusercontent.com/. . ./my_file.csv?token=XXXXXXXXXXXXXXXXXX

Is there a way to change the filename of a file, when starting to download a file using Selenium?

I'd like to download multiple files from a single website, but the biggest quirk I have is that the server automatically generates a random filename upon requesting the file to download. The issue here is then I won't know which file is which, without having to manually go through each file. However, on the site that has the links to download the files, they all have a name. For example...
File name -> Resultant file name(fake file names)
Week1.pdf 2asd123e.pdf
Week1_1.jpg dsfgp142.jpg
.
.
Week10.pdf 19fgmo2o.pdf
Week11.pdf 0we5984w.pdf
If I were to download them manually by myself, I would type click "download" and a popup "Save as" menu comes up, which gives me the option to change the file name manually, then click ok to confirm the download, to which it starts downloading.
Currently, my code is made to open up the website, log into my account, go to the files page, and then find a file, with it's corresponding server request link. IE: . I am able to store the name of the file, "Week1.pdf" into a variable, and click on the request link, but the only problem is that the Save as menu, doesn't have the ability to change the name of the filename, and only gives me the option to view the file, or Save the file immediately. I've looked around a little, and tried to play around with the Firefox profile settings, but nothing has worked. How would I go about solving this problem?
Thanks
I can think of a few things that you might try...
After the file is saved, look in the downloads folder for the most recently saved file (with the correct extension) using time stamps. This will probably be OK as long as you aren't running this threaded.
Get the list of files in the download directory, download the file, find the file that doesn't exist in the list of files. Again this should be safe unless you are running this threaded.
Create a new folder, set the download directory to the newly created folder, download the file. It should be the only file in that directory. As far as I know, you can only set the download directory before creating the driver instance.
In each of these cases, if you plan to download multiple files I would rename each file as you download them or move them into some known directory to make it easier on yourself.
There's another method I ran across in another answer.

Useing python to download a file in google drive that is created by a google app script

Ok so I've got a google script that I'm working on to help out with some stuff and I need to be able to create a txt file and download it to my local computer. I've figured out how to create the file as a blob and create a file in google drive.
The problem is that I for one can't figure out how to delete the old file and create the new file and I also can't seem to figure out how to download it locally so I can work my python magic and create a nice looking report and print it out. I've gone over the documentation and looked at similar questions but I can't figure out how to actually download a file.
An example program would be great answer for me something that uses a dummy file and downloads it, really that would be awesome.
My thoughts are that I could go back to the old file that I am trying to download and just edit it so that way I don't have to actually delete the file which would make it have the same ID's, meta data, and URL.
Can I use the google app script to download it directly to my computer and for a little added info I run a Linux machine so some things are a little more labor intensive for me while some other stuff is nice and easy. I feel like there is an app that can run on my computer locally that stores my google drive files locally and I could possibly just grab it from there.
Lastly a link to documentation for running the scripts natively would be helpful as well.
This is how to download a file in google drive using Python.
# sample
import googleDriveAccess
ci = googleDriveAccess.readClientId(basedir)
ds = googleDriveAccess.second_authorize(basedir, ci, script=True)
id, fileobj = googleDriveAccess.script_download(ds, folder, SCRIPT_ID)
source code of googleDriveAccess:
https://github.com/HatsuneMiku/googleDriveAccess
Apps Script Crash Course: Import/Export Apps Script Code
https://www.youtube.com/watch?v=lEVMu9KE6jk
Google Drive SDK: Searching for files
https://www.youtube.com/watch?v=DOSvQmQK_HA
use
urllib.retrieve(httpaddressofthefile, filenametosaveitto)
or
import urllib2
response = urllib2.urlopen('http://www.wherever.com/')
html = response.read()
f=open(filename, 'w')
f.write(html)
f.close()
Okay what you are looking for is pydrive
Its a google drive API made just for uploading/downloading google drive stuff.
also #example request:
here:
#-------------------------------------------------------------------------------
# Author: Amazingred
# Created: 0017032814
#-------------------------------------------------------------------------------
import urllib, urllib2, os, sys
#The pydrive package download
page='https://pypi.python.org/packages/source/P/PyDrive/PyDrive-1.0.0.tar.gz'
request=urllib.urlretrieve(page, 'pydrive.tar.gz')
if os.path.exists(os.path.join(os.path.dirname(__file__),"pydrive.tar.gz")):
print "YUP THE FILE EXISTS"
else:
print "nope"

How to upload files to an HTTP location using a python script

I am looking for a script which will upload files from a unix server to an HTTP location.
I want to use only these modules - cookielib, urllib2, urllib, mimetypes, mimetools, traceback
I would like to make question more clear
here http location mentioned is a common share folder link which can use by number of people by accessing through username/passwd.
for ex: my target location has:
I have http:/commonfolder.mydomain.com/somelocation/345333 location
I have username = someuid ,password = xxxxx
my source location has:
I have a my.txt file contains some data
I want to run shell or python script which will tranfer the file to target location mentioned above. so that many people can start using latest info I am uploading timely.
-shijo

Categories