Downloading a file using the Dropbox Python library - python

Environment: Windows 7, Python Tools for Visual Studio, Python 2.7, Python Package dropbox(6.9.0), Access Token from my Dropbox account
The following code is run:
import dropbox
access_token = '<token value here>'
dbx = dropbox.Dropbox(access_token)
with open("C:\Test.txt", "w") as f:
metadata, res = dbx.files_download(path="/Test.txt")
f.write(res.content)
It errors on the last line with the following:
"No disassembly available"
I don't understand the error not being a Python developer.. the file is created on the local machine but nothing is downloaded into it from the dropbox file..
Any help would be greatly appreciated.. Thanks

python code for dropbox download with business API:
def dropbox_file_download(access_token,dropbox_file_path,local_folder_name):
try:
dropbox_file_name = dropbox_file_path.split('/')[-1]
dropbox_file_path = '/'.join(dropbox_file_path.split('/')[:-1])
dbx = dropbox.DropboxTeam(access_token)
# get the team member id for common user
members = dbx.team_members_list()
for i in range(0,len(members.members)):
if members.members[i].profile.name.display_name == logged_user_name:
member_id = members.members[i].profile.team_member_id
break
# connect to dropbox with member id
dbx = dropbox.DropboxTeam(access_token).as_user(member_id)
# list all the files from the folder
result = dbx.files_list_folder(dropbox_file_path, recursive=False)
# download given file from dropbox
for entry in result.entries:
if isinstance(entry, dropbox.files.FileMetadata):
if entry.name == dropbox_file_name:
dbx.files_download_to_file(local_folder_name+entry.name, entry.path_lower)
return True
return False
except Exception as e:
print(e)
return False

import dropbox
access_token = '**********************'
dbx = dropbox.Dropbox(access_token)
f = open("ABC.txt","w")
metadata,res = dbx.files_download("abc.txt") //dropbox file path
f.write(res.content)

Related

Dropbox Python SDK , unable to download or list files

We are planning to use Dropbox to get some csv files transfered from client location. This csv files need to be processed by a data engineering pipeline. We are using python to process this. As a first step , we need to download the file from dropbox to the local file folder. I have created an app using Dropbox App Console as Scoped App first. In the Python program we need to get an API Access token. And from the scopped App , I was not able to generate the Access token as I was getting error stating that " You need to be a Team administrator to generate the token". This was misleading as this was a single account i created for testing it out and no teams are present. I tried with another method which is using the user id and secret to prompt for an access token
here is the code :
class DropboxFolderCreation:
"""
This class is responsible for creating empty directories in dropbox account.
"""
def __init__(self):
# define your dropbox app key below
self.app_key = 'xxxxxxxxxxx'
# define your dropbox app secret key below
self.app_secret = 'xxxxxxxxxxxxx'
# define your CSV file path below
self.csv_path = 'example.csv'
def login_dropbox(self):
"""
Authorise Dropbox using OAuth 2.0
Follow instructions and authorise your Dropbox account.
"""
APP_KEY = self.app_key
APP_SECRET = self.app_secret
auth_flow = dropbox.DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)
authorize_url = auth_flow.start()
print ("1. Go to: " + authorize_url)
print ("2. Click \"Allow\" (you might have to log in first).")
print ("3. Copy the authorization code.")
auth_code = input("Enter the authorization code here: ").strip()
try:
oauth_result = auth_flow.finish(auth_code)
except Exception as e:
print("Error: %s" % (e,))
return oauth_result
def read_csv(self,dbx):
"""
read .csv file and extract directory names
"""
"""wb = open_workbook(self.csv_path).sheet_by_index(0)
directory_list = []
# if csv file contains data from row 2 then set start_rowx = 1
# else set it to 0 as below
# first argument is set to 0 since we want to read column 1 of csv
csv_data = wb.col_values(0, start_rowx = 0)"""
#dbx = dropbox.Dropbox(<access_token>)
metadata, f = dbx.files_download(self.csv_path)
print(metadata)
csv_reader = csv.reader(f.content.decode().splitlines(), delimiter=',')
with open(metadata) as file:
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
print(row)
line_count += 1
print(f'Processed {line_count} lines.')
return csv_data
def create_dirs_on_dropbox(self):
"""
Create empty directories in Dropbox account using API v2
"""
token = self.login_dropbox()
dbx = dropbox.Dropbox(token.access_token)
dirs = self.read_csv(dbx)
csv_data = self.read_csv(dbx)
if csv_data:
#doing something here
print("Successfully download file from your dropbox account ")
else:
print("could not read data from csv file")
And when executing the below :
dbx_instance = DropboxFolderCreation()
dbx_instance.create_dirs_on_dropbox()
1. Go to: https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=a9hg3hhu85613yv
2. Click "Allow" (you might have to log in first).
3. Copy the authorization code.
Enter the authorization code here: dTuX5n5_iV0AAAAAAAAAKX5Rrskr-ZCroPMjuSK2qMo
Connection to Dropbox is successful , but getting error while trying to access the file
error as :
ValidationError: 'ListPriceChanges.csv' did not match pattern '(/(.|[\r\n])*|id:.*)|(rev:[0-9a-f]{9,})|(ns:[0-9]+(/.*)?)'
-I suspected this error is coming because I am not able to read the folder list which I verified using this
response = dbx.files_list_folder(path="")
print(response)
which returns an empty list.
So My problem is how to generate the access token for the scoped App . Do we have any simple way to connect and download the files ?
We have similar issues . When you use scope application, do not select any scope related to teams ,
from the App console, select your application, Click on the scoped App, and deselect everything under the team scope
you can come back and generate the access token now.
Once you have access token for your scoped App, then it is pretty straight forward
import dropbox
dbx = dropbox.Dropbox("access token")
with open("example.csv", "wb") as f:
metadata, res = dbx.files_download(path="/example.csv")
f.write(res.content)
The above code will download the example.csv from the root folder . If you have the file under any folder say test then in the path you need to specify /test/example.csv

Upload a file to a Sharepoint folder using Python

I have a Python script that saves a file to a server shared folder for a user to access. Our organization recently moved our server file structure to Sharepoint... including all the folders. (I've read multiple articles that's a bad idea, but there's no changing it right now.)
I have new code that uploads a file to the root folder of the Sharepoint library:
import sharepy
s = sharepy.connect("site.sharepoint.com", "username", "password")
r = s.post("https://site.sharepoint.com/_api/web/Lists/GetByTitle('Documents')/RootFolder/files\
/add(overwrite=true,url='test.csv')", \
"testing,foo,bar")
print(r)
Is there a way to upload the file to a subfolder instead of the root? If so, how?
I've worked on the same problem some time back. Below is my code.
import requests
from shareplum import Office365
username = "YourUsername"
password = "YourPassword"
site_name = "Sitename"
doc_library = "SubfolderName"
base_path = "https://domainName.sharepoint.com"
file_name = "FileName"
# Obtain auth cookie
authcookie = Office365(base_path, username=username, password=password).GetCookies()
session = requests.Session()
session.cookies = authcookie
session.headers.update({'user-agent': 'python_bite/v1'})
session.headers.update({'accept': 'application/json;odata=verbose'})
# dirty workaround.... I'm getting the X-RequestDigest from the first failed call
session.headers.update({'X-RequestDigest': 'FormDigestValue'})
response = session.post( url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='a.txt',overwrite=true)",
data="")
session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})
dest = base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='a.txt',overwrite=true)" #session.post( url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='a.txt',overwrite=true)",data="")
print('Folder!')
# perform the actual upload
with open( file_name, 'rb') as file_input:
try:
print('uploading')
response = session.post(
url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='"
+ file_name + "',overwrite=true)",
data=file_input)
except Exception as err:
print("Some error occurred: " + str(err))
print('Uploaded successfully!')
In case it helps anybody, here's my final code. It successfully posts a file to a sharepoint site team site library subfolder. Replace the italics with your information.
import sharepy
s = sharepy.connect("*MySite*.sharepoint.com", "*username*", "*password*")
r = s.post("https://*MySite*.sharepoint.com/sites/*TeamSiteName*/_api/web/GetFolderByServerRelativeUrl('/sites/*TeamSiteName*/Shared Documents/*FolderName*')/Files/" + "add(overwrite=true,url='test.csv')", "testing,foo,bar")
print r
Below is the code I used to upload files from Azure Blob storage to a new sub folder on Sharepoint using python in Databricks.
def upload_sharepoint(sp_filepath,blob_file_path):
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import os
url='https://<domain>.sharepoint.com/sites/<site1>/<subsite1>'
username = 'user'
pwd = 'password'
ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, pwd)
ctx = ClientContext(url, ctx_auth)
blobpath = '/dbfs' + blob_file_path
filename=os.path.basename(blob_file_path)
#read content of file
with open(blobpath, 'rb') as content_file:
file_content = content_file.read()
target_url = sp_filepath # sharepoint url to upload a file
target_folder=ctx.web.get_folder_by_server_relative_url(target_url)
try:
folder_exist=ctx.load(target_folder).execute_query().properties['Exists']
except Exception as e:
folder_exist=False
print('Folder Not Found,Creating Folder')
if !folder_exist:
try:
target_folder = ctx.web.folders.add(target_url).execute_query() #Create the folder, can create folder upto 1 level only
except Exception as e:
print('Parent folder Not Found',e)
target_folder.upload_file(filename, file_content).execute_query() # upload the file
print('Uploaded '+filename+' to '+target_url)
The above code can be used to create a 1 level sub-folder within a folder that already exist.
For Ex, here we will create a folder name 'NewFolder' inside 'Pay file' folder that exist on Sharepoint:
sharepoint_fp='/sites/<site1>/<subsite1>/Document%20upload/Pay%20file/NewFolder'
blob_path='/mnt/PayFile/'
files=spark.createDataFrame(dbutils.fs.ls(blob_path))
files=files.select('name').collect()
for f in files:
upload_sharepoint(sharepoint_fp,blob_path+f.name)
Yes you can upload files to sub-folder via rest api. Please take a reference of following endpoints:
Add method to create a file
create a file and add it to a folder
And below are some demos about how to upload files using python (but they might not use the same library as yours).
SharePlum
Office365-REST-Python-Client
/////// Update //////
After so many tries, I finally made this work with less line coding than I expected.
I was having some issues with the some urls and folder paths that's why I put the "r" to get the raw path.
from shareplum import Site
from shareplum import Office365
from shareplum.site import Version
Normal authentication
authcookie = Office365(r'https://.sharepoint.com', username='#', password='***').GetCookies()
Here you fill the info from your site
site = Site(r'https://*******.sharepoint.com/sites/sitename/',version=Version.v365, authcookie=authcookie)
And here you just fill the info of your folder
folder = site.Folder('Shared Documents/Your Folder')
Source: https://shareplum.readthedocs.io/en/latest/files.html#folders
Encoding and Errors are optional
with open(r'C:/Users/Alessandro.paiva/Desktop/file.xlsx', encoding = 'latin-1', errors = 'ignore') as file:
fileContent = file.read()
Name of the file
folder.upload_file(fileContent, r'file.xlsx')

Python - uploading a zip folder to dropbox via APIv2 keeps failing

I am trying to upload a zip folder to dropbox. The zip is a backup, with a custom name according to the current date time. The folder is correctly zipped, named and stored. Sadly, there is always an issue uploading to dropbox. I have tested a readme.txt using the same code, which works. I don’t understand where this is going wrong. Thanks for any help.
import dropbox
import os
import datetime
dt = ('{:%Y%m%d_%H%M}'.format(datetime.datetime.now()))
name = dt + "_pa.zip"
os.system("zip -r " + name + " /home/obliss")
class TransferData:
def __init__(self, access_token):
self.access_token = access_token
def upload_file(self, file_from, file_to):
dbx = dropbox.Dropbox(self.access_token)
with open(file_from, 'rb') as f:
dbx.files_upload(f.read(), file_to, mode=dropbox.files.WriteMode.overwrite)
access_token = "[hidden]"
file_from = "/home/olbliss/"+name
file_to = "/Work/Python Anywhere Backups/"+name
transferData = TransferData(access_token)
try:
transferData.upload_file(file_from, file_to)
except:
os.remove(name)
print('uploaded failed, '+name+' removed from /home/olbliss/')
try:
os.remove(name)
except:
pass
Failure message:
The 413 status code indicates that the payload was too large. The files_upload method only officially supports files up to 150 MB in size. You'll need to use upload_sessions for larger files.
Here's a basic example that uses the Dropbox Python SDK to upload a file to the Dropbox API from the local file as specified by file_path to the remote path as specified by dest_path. It also chooses whether or not to use an upload session based on the size of the file:
f = open(file_path)
file_size = os.path.getsize(file_path)
CHUNK_SIZE = 8 * 1024 * 1024
if file_size <= CHUNK_SIZE:
print dbx.files_upload(f.read(), dest_path)
else:
upload_session_start_result = dbx.files_upload_session_start(f.read(CHUNK_SIZE))
cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id,
offset=f.tell())
commit = dropbox.files.CommitInfo(path=dest_path)
while f.tell() <= file_size:
if ((file_size - f.tell()) <= CHUNK_SIZE):
print dbx.files_upload_session_finish(f.read(CHUNK_SIZE),
cursor,
commit)
break
else:
dbx.files_upload_session_append_v2(f.read(CHUNK_SIZE),
cursor)
cursor.offset = f.tell()
f.close()
Note: this should only serve as an example. It hasn't been extensively tested and doesn't implement error handling.

upload file to my dropbox from python script

I want to upload a file from my python script to my dropbox account automatically. I can't find anyway to do this with just a user/pass. Everything I see in the Dropbox SDK is related to an app having user interaction. I just want to do something like this:
https://api-content.dropbox.com/1/files_put//?user=me&pass=blah
The answer of #Christina is based on Dropbox APP v1, which is deprecated now and will be turned off on 6/28/2017. (Refer to here for more information.)
APP v2 is launched in November, 2015 which is simpler, more consistent, and more comprehensive.
Here is the source code with APP v2.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import dropbox
class TransferData:
def __init__(self, access_token):
self.access_token = access_token
def upload_file(self, file_from, file_to):
"""upload a file to Dropbox using API v2
"""
dbx = dropbox.Dropbox(self.access_token)
with open(file_from, 'rb') as f:
dbx.files_upload(f.read(), file_to)
def main():
access_token = '******'
transferData = TransferData(access_token)
file_from = 'test.txt'
file_to = '/test_dropbox/test.txt' # The full path to upload the file to, including the file name
# API v2
transferData.upload_file(file_from, file_to)
if __name__ == '__main__':
main()
The source code is hosted on GitHub, here.
Important Note: this answer is deprecated since dropbox uses v2 API now.
See the answer of #SparkAndShine for current API version solution
Thanks to #smarx for the answer above! I just wanted to clarify for anyone else trying to do this.
Make sure you install the dropbox module first of course, pip install dropbox.
Create an app under your own dropbox account in the "App Console". (https://www.dropbox.com/developers/apps)
Just for the record I created my App with the following:
a. App Type as "Dropbox API APP".
b. Type of data access as "Files & Datastores"
c. Folder access as "My app needs access to files already on Dropbox". (ie: Permission Type as "Full Dropbox".)
Then click the "generate access token" button and cut/paste into the python example below in place of <auth_token>:
import dropbox
client = dropbox.client.DropboxClient(<auth_token>)
print 'linked account: ', client.account_info()
f = open('working-draft.txt', 'rb')
response = client.put_file('/magnum-opus.txt', f)
print 'uploaded: ', response
folder_metadata = client.metadata('/')
print 'metadata: ', folder_metadata
f, metadata = client.get_file_and_metadata('/magnum-opus.txt')
out = open('magnum-opus.txt', 'wb')
out.write(f.read())
out.close()
print metadata
Here's my approach using API v2 (and Python 3). I wanted to upload a file and create a share link for it, which I could email to users. It's based on sparkandshine's example. Note I think the current API documentation has a small error which sparkandshine has corrected.
import pathlib
import dropbox
import re
# the source file
folder = pathlib.Path(".") # located in this folder
filename = "test.txt" # file name
filepath = folder / filename # path object, defining the file
# target location in Dropbox
target = "/Temp/" # the target folder
targetfile = target + filename # the target path and file name
# Create a dropbox object using an API v2 key
d = dropbox.Dropbox(your_api_access_token)
# open the file and upload it
with filepath.open("rb") as f:
# upload gives you metadata about the file
# we want to overwite any previous version of the file
meta = d.files_upload(f.read(), targetfile, mode=dropbox.files.WriteMode("overwrite"))
# create a shared link
link = d.sharing_create_shared_link(targetfile)
# url which can be shared
url = link.url
# link which directly downloads by replacing ?dl=0 with ?dl=1
dl_url = re.sub(r"\?dl\=0", "?dl=1", url)
print (dl_url)
import dropbox
access_token = '************************'
file_from = 'index.jpeg' //local file path
file_to = '/Siva/index.jpeg' // dropbox path
def upload_file(file_from, file_to):
dbx = dropbox.Dropbox(access_token)
f = open(file_from, 'rb')
dbx.files_upload(f.read(), file_to)
upload_file(file_from,file_to)
The only way to authenticate calls to the Dropbox API is to use OAuth, which involves the user giving permission to your app. We don't allow third-party apps to handle user credentials (username and password).
If this is just for your account, note that you can easily get an OAuth token for your own account and just use that. See https://www.dropbox.com/developers/blog/94/generate-an-access-token-for-your-own-account.
If this is for other users, they'll need to authorize your app once via the browser for you to get an OAuth token. Once you have the token, you can keep using it, though, so each user should only have to do this once.
Sorry if im missing something but cant you just download the dropbox application for your OS and then save the file (in windows) in:
C:\Users\<UserName>\Dropbox\<FileName>
i just ceated a python program to save a text file, checked my dropbox and it saves them fine.
For Dropbox Business API below python code helps uploading files to dropbox.
def dropbox_file_upload(access_token,dropbox_file_path,local_file_name):
'''
The function upload file to dropbox.
Parameters:
access_token(str): Access token to authinticate dropbox
dropbox_file_path(str): dropboth file path along with file name
Eg: '/ab/Input/f_name.xlsx'
local_file_name(str): local file name with path from where file needs to be uploaded
Eg: 'f_name.xlsx' # if working directory
Returns:
Boolean:
True on successful upload
False on unsuccessful upload
'''
try:
dbx = dropbox.DropboxTeam(access_token)
# get the team member id for common user
members = dbx.team_members_list()
for i in range(0,len(members.members)):
if members.members[i].profile.name.display_name == logged_in_user:
member_id = members.members[i].profile.team_member_id
break
# connect to dropbox with member id
dbx = dropbox.DropboxTeam(access_token).as_user(member_id)
# upload local file to dropbox
f = open(local_file_name, 'rb')
dbx.files_upload(f.read(),dropbox_file_path)
return True
except Exception as e:
print(e)
return False
If you need to upload a BIG file, you need to break up the file into chunks and upload the chunks one by one as follows. Inspired by this great medium artcle:
def upload_a_big_file(local_file_path: str, remote_file_path: str):
# grab your authenticated client
dbx = get_dropbox_client()
file_size = os.path.getsize(local_file_path)
# Upload 8 MB chunks at a time
CHUNK_SIZE = 8 * 1024 * 1024
with open(local_file_path, 'rb') as local_file:
uploaded_size = 0
upload_session_start_result = dbx.files_upload_session_start(local_file.read(CHUNK_SIZE))
cursor = dropbox.files.UploadSessionCursor(
session_id=upload_session_start_result.session_id,
offset=local_file.tell()
)
commit = dropbox.files.CommitInfo(
path=remote_file_path,
mode=dropbox.files.WriteMode.overwrite
)
print("Starting Upload.")
while local_file.tell() <= file_size:
if ((file_size - local_file.tell()) <= CHUNK_SIZE):
# Last chunk remaining, so commit
dbx.files_upload_session_finish(
local_file.read(CHUNK_SIZE),
cursor,
commit
)
print("Done uploading !")
break
else:
dbx.files_upload_session_append_v2(
local_file.read(CHUNK_SIZE),
cursor
)
cursor.offset = local_file.tell()
uploaded_size += CHUNK_SIZE
uploaded_percent = 100*uploaded_size/file_size
print('Uploaded {:.2f}%'.format(uploaded_percent))
Here is the code for uploading livevideo on dropbox using python in windows.
Hope this will help you.
import numpy as np
import cv2
import dropbox
import os
from glob import iglob
access_token = 'paste your access token here' #paste your access token in-between ''
client = dropbox.client.DropboxClient(access_token)
print 'linked account: ', client.account_info()
PATH = ''
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('C:\python27\output1.avi',fourcc, 20.0, (640,480))
#here output1.avi is the filename in which your video which is captured from webcam is stored. and it resides in C:\python27 as per the path is given.
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
#frame = cv2.flip(frame,0) #if u want to flip your video
# write the (unflipped or flipped) frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
for filename in iglob(os.path.join(PATH, 'C:/Python27/output1.avi')):
print filename
try:
f = open(filename, 'rb')
response = client.put_file('/livevideo1.avi', f)
print "uploaded:", response
f.close()
#os.remove(filename)
except Exception, e:
print 'Error %s' % e
Here is the code for uploading existing video on your dropbox account using python in windows.
Hope this will help you.
# Include the Dropbox SDK
import dropbox
# Get your app key and secret from the Dropbox developer website
app_key = 'paste your app-key here'
app_secret = 'paste your app-secret here'
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
# Have the user sign in and authorize this token
authorize_url = flow.start()
print '1. Go to: ' + authorize_url
print '2. Click "Allow" (you might have to log in first)'
print '3. Copy the authorization code.'
code = raw_input("Enter the authorization code here: ").strip()
# This will fail if the user enters an invalid authorization code
access_token, user_id = flow.finish(code)
client = dropbox.client.DropboxClient(access_token)
print 'linked account: ', client.account_info()
f = open('give full path of the video which u want to upload on your dropbox account(ex: C:\python27\examples\video.avi)', 'rb')
response = client.put_file('/video1.avi', f) #video1.avi is the name in which your video is shown on your dropbox account. You can give any name here.
print 'uploaded: ', response
folder_metadata = client.metadata('/')
print 'metadata: ', folder_metadata
f, metadata = client.get_file_and_metadata('/video1.avi')
out = open('video1.avi', 'wb')
out.write(f.read())
out.close()
print metadata
Now for uploading images, the same code will be used.
Only write your image file name which you want to upload for ex: image.jpg in place of video name . Also change the name of video1.avi and write name for image in which your uploaded image will be shown in your dropbox for ex:image1.jpg.

.xlsx and xls(Latest Versions) to pdf using python

With the help of this .doc to pdf using python
Link I am trying for excel (.xlsx and xls formats)
Following is modified Code for Excel:
import os
from win32com import client
folder = "C:\\Oprance\\Excel\\XlsxWriter-0.5.1"
file_type = 'xlsx'
out_folder = folder + "\\PDF_excel"
os.chdir(folder)
if not os.path.exists(out_folder):
print 'Creating output folder...'
os.makedirs(out_folder)
print out_folder, 'created.'
else:
print out_folder, 'already exists.\n'
for files in os.listdir("."):
if files.endswith(".xlsx"):
print files
print '\n\n'
word = client.DispatchEx("Excel.Application")
for files in os.listdir("."):
if files.endswith(".xlsx") or files.endswith('xls'):
out_name = files.replace(file_type, r"pdf")
in_file = os.path.abspath(folder + "\\" + files)
out_file = os.path.abspath(out_folder + "\\" + out_name)
doc = word.Workbooks.Open(in_file)
print 'Exporting', out_file
doc.SaveAs(out_file, FileFormat=56)
doc.Close()
It is showing following error :
>>> execfile('excel_to_pdf.py')
Creating output folder...
C:\Excel\XlsxWriter-0.5.1\PDF_excel created.
apms_trial.xlsx
~$apms_trial.xlsx
Exporting C:\Excel\XlsxWriter-0.5.1\PDF_excel\apms_trial.pdf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "excel_to_pdf.py", line 30, in <module>
doc = word.Workbooks.Open(in_file)
File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel
', u"Excel cannot open the file '~$apms_trial.xlsx' because the file format or f
ile extension is not valid. Verify that the file has not been corrupted and that
the file extension matches the format of the file.", u'xlmain11.chm', 0, -21468
27284), None)
>>>
There is problem in
doc.SaveAs(out_file, FileFormat=56)
What should be FileFormat file format?
Please Help
Link of xlsxwriter :
https://xlsxwriter.readthedocs.org/en/latest/contents.html
With the help of this you can generate excel file with .xlsx and .xls
for example excel file generated name is trial.xls
Now if you want to generate pdf of that excel file then do the following :
from win32com import client
xlApp = client.Dispatch("Excel.Application")
books = xlApp.Workbooks.Open('C:\\excel\\trial.xls')
ws = books.Worksheets[0]
ws.Visible = 1
ws.ExportAsFixedFormat(0, 'C:\\excel\\trial.pdf')
I got the same thing and the same error... ANSWER: 57.... see below...
from win32com import client
import win32api
def exceltopdf(doc):
excel = client.DispatchEx("Excel.Application")
excel.Visible = 0
wb = excel.Workbooks.Open(doc)
ws = wb.Worksheets[1]
try:
wb.SaveAs('c:\\targetfolder\\result.pdf', FileFormat=57)
except Exception, e:
print "Failed to convert"
print str(e)
finally:
wb.Close()
excel.Quit()
... as an alternative to the fragile ExportAsFixedFormat...
You can print an excel sheet to pdf on linux using python.
Do need to run openoffice as a headless server and use unoconv, takes a bit of configuring but is doable
You run OO as a (service) daemon and use it for the conversions for xls, xlsx and doc, docx.
http://dag.wiee.rs/home-made/unoconv/
Another solution for
Is to start gotenberg docker container locally
https://github.com/gotenberg/gotenberg
And pass (any supported by libreoffice) file from python wia HTTP to the container and get result as pdf
LIBREOFFICE_URL = 'http://localhost:3000/forms/libreoffice/convert'
LIBREOFFICE_LANDSCAPE_URL = 'http://localhost:3000/forms/libreoffice/convert?landscape=1'
def _retry_gotenberg(url, io_bytes, post_file_name='index.html'):
response = None
for _ in range(5):
response = requests.post(url, files={post_file_name: io_bytes})
if response.status_code == 200:
break
logging.info('Will sleep and retry: %s %s', response.status_code, response.content)
sleep(3)
if not response or response.status_code != 200:
raise RuntimeRrror(f'Bad response from doc-to-pdf: {response.status_code} {response.content}')
return response
def process_libreoffice(io_bytes, ext: str):
if ext in ('.doc', '.docx'):
url = LIBREOFFICE_URL
else:
url = LIBREOFFICE_LANDSCAPE_URL
response = self._retry_gotenberg(url, io_bytes, post_file_name=f'file.{ext}')
return response.content
The GroupDocs.Conversion Cloud SDK for Python is another option to convert Excel to PDF. It is paid API. However, it provides 150 free monthly API calls.
P.S: I'm a developer evangelist at GroupDocs.
# Import module
import groupdocs_conversion_cloud
from shutil import copyfile
# Get your client_id and client_key at https://dashboard.groupdocs.cloud (free registration is required).
client_id = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx"
client_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Create instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_key)
try:
#Convert PDF to PNG
# Prepare request
request = groupdocs_conversion_cloud.ConvertDocumentDirectRequest("pdf", "C:/Temp/Book1.xlsx")
# Convert
result = convert_api.convert_document_direct(request)
copyfile(result, 'C:/Temp/Book1_output.pdf')
print("Result {}".format(result))
except groupdocs_conversion_cloud.ApiException as e:
print("Exception when calling get_supported_conversion_types: {0}".format(e.message))

Categories

Resources