Exporting Python Data to Excel File on Sharepoint - python

I have a dataframe that I want to export to an existing Excel file that exists online on my company's sharepoint. I have the following line of code to do this:
df.to_excel(r'Path where the exported excel file will be stored\File Name.xlsx', sheet_name='Your sheet name', index = False)
The filepath starts with https and ends with a filetype .xlsx?web=1
Python of course doesn't recognise .xlsx?web=1 as a valid filetype so do I just ignore the ?web=1 at the end? This is what I did but get an access denied error. What do I need to do to be able to export to this file?
Edit: Filepath is https://companyname.sharepoint.com/sites/XXX/Shared%20Documents/07%20-%20LE%20(Loss%20Elimination)/02%20-%20Open%20(Current)%20Loss%20Elimantions/13%20-%20Material%20loss%20against%20BOM/Copy_Material%20Flow%20for%20HDW%20Euro%20When%20to%20FP.xlsx?web=1

The filepath starts with https and ends with a filetype .xlsx?web=1
That's not a path, that's a URL.
You cannot simply "save" to a URL.
There are Python libraries for working with sharepoint. For example Office365-REST-Python-Client, pysharepoint and this one on pypi, but the latter only seems to support downloading, not uploading.
Since pysharepoint is basically a front-end for SharePlum, you might want to look at that as well. It comes with documentation.

Related

xlwings to write a text file locally

Hi I am looking to export data from the excel workbook out to my desktop as a text or an XML file.
I have it formed into an XML file and I am writing it locally when in DEBUG mode but when I add in the UDF and try run the same code it wont save the file.
Anyone any ideas as to why this wont work.
I needed to specify the Absolute Path for the creation of the file.
further information can be found here
xlwings calls a python function that should create a file, but no file get created

openpyxl corrupts spreadsheet if it contains a data source

I use openpyxl to interact with Excel files using Python 3.7. I open and save my .xlsx spreadsheets as follows:
from openpyxl import load_workbook
wb.load_workbook('file.xlsx', read_only=False)
wb.save('file.xlsx')
If file.xlsx contains no links to external data sources (such as SQL Server or Postgre-SQL), then there is no problem with the saved file and it opens okay in Excel after being processed by my Python script.
However, if file.xlsx does contain a link to external data, then upon executing the above script, the output file is now corrupted. When opening the file in Excel, the following error is reported and I have the option of attempting to recover it. When recovering, the data remains but all links to the data source are gone.
> We found a problem with some content in file.xlsx. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.
It is easy to reproduce this error as follows:
Create a blank spreadsheet and save it as file.xlsx.
Run the above three lines of Python code to open and save the file. You will see this works fine and has no impact on the spreadsheet.
Now open file.xlsx in Excel and, from the Data tab, choose a data source. You can choose any data source (link to a csv file, a table within Excel, or an external data source - it doesn't matter).
Save the spreadsheet, then run the above Python script (which again, simply opens and saves it).
Open file.xlsx in Excel. You will see that it is now corrupted.
My conclusion is that, at the moment, openpyxl doesn't support spreadsheets that contain links to external data. It would be useful to have this confirmed, or for a workaround to the above issue to be proposed.
Thanks!!

Outputting A .xls File In Python

I have been teaching myself Python to automate some of our work processes. So far reading from Excel files (.xls, .xlsx) has gone great.
Currently I have hit a bit of a snag. Although I can output .xlsx files fine, the software system that we have to use for our primary work task can only take .xls files as an input - it cannot handle .xlsx files, and the vendor sees no reason to add .xlsx support at any point in the foreseeable future.
When I try to output a .xls file using either Pandas or OpenPyXl, and open that file in Excel, I get a warning that the file format and extension of the file do not match, which leads me to think that attempting to open this file using our software could lead to some pretty unexpected consequences (because it's actually a .xlsx file, just not named as such)
I've tried to search for how to fix this all on Google, but all I can find are guides for how to convert a .xls file to a .xlsx file (which is almost the opposite of what I need). So I was wondering if anybody could please help me on whether this can be achieved, and if it can, how.
Thank you very much for your time
Under the pandas.DataFrame.to_excel documentation you should notice a parameter called engine, which states:
engine : str, optional
Write engine to use, openpyxl or xlsxwriter. You can also set this via the options io.excel.xlsx.writer, io.excel.xls.writer, and io.excel.xlsm.writer.
What it does not state is that the engine param is automatically picked based on your file extension -- therefore, easy fix:
import pandas as pd
df = pd.DataFrame({"data": [1, 2, 3]})
df.to_excel("file.xls") # Notice desired file extension.
This will automatically use the xlwt engine, so make sure you have it installed via pip install xlwt.
Felipe is right the filename extension will set the engine parameter.
So basically all it's saying is that the old Excel format ".xls" extension is no longer supported in Pandas. So if you specify the output spreadsheet with the ".xlsx" extension the warning message disappears.
I FINALLY have the answer!
I have libreoffice installed and am using the following in the command line on windows:
"C:\Program Files\LibreOffice\program\soffice.exe" --headless --convert-to xlsx test2.xls
Currently trying to use subprocess to automate this.

How to fix [Errno13] permission denied when trying to read excel file?

I tried the following code to be able to read an excel file from my personal computer.
import xlrd
book = xlrd.open_workbook('C:\\Users\eline\Documents\***\***\Python', 'Example 1.xlsx')
But I am getting the error 'Permission denied'. I am using windows and if I look at the properties of the directory and look at the 'Security' tab I have three groups/users and all three have permissions for all the authorities, except for the last option which is called 'special authorities' (as far as I know I do not need this authority to read the excel file in Python).
I have no idea how to fix this error. Furthermore, I do not have the Excel file open on my computer when running the simulation.
I really hope someone can help me to fix this error.
Sometimes, it is because you try to read the Excel file while it is opened. Close the file in Excel and you are good to go.
book = xlrd.open_workbook('C:\\Users\eline\Documents\***\***\Python', 'Example 1.xlsx')
You cannot give path like this to xlrd. path need to be single string.
If you insist you can use os module
import os
book = xlrd.open_workbook(os.path.join('C:\\Users\eline\Documents\***\***\Python', 'Example 1.xlsx'))
[Errno13] permission denied in your case is happening because you want to read folder like a file which is not allowed.
I ran into this situation also while reading an Excel file into a data frame. To me it appears that it is a Python and/or Excel bug which we should probably not hide by using os.path.join even if that solves the problem. My situation involved an excel spreadsheet that links cells to another CSV file. If this excel file is freshly opened and open when I try to read it in python, it fails.
Python reads it correctly if I do an unnecessary save of the open Excel file.

Error opening text file saved with .xls extension in python

I'm using labview to create and save data from an experiment. Labview itself creates a text file but saves it automatically with a .xls extension (word 1997-2003--it's an old setup that was never changed because it never broke). Whenever I go to open one of the data files, excel spits out this:
"The file you are trying to open, 'name.ext', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"
I'm generating a lot of data, so I want to use python to sort it out and do some quick analysis over files in a directory.
The problem is that python doesn't like that it's a text file saved with a .xls extension. It can cycle through the directory just fine to get the file names, but whenver I actually try to open the file or do anything with it, I get the error in the image attached. This happens if I change the extension to .xls, .xlsx, or do nothing with it at all and let it try to open the original filename.
error message
I literally have hundreds of these .xls files. I know I can go through, open each one in excel and save as a real excel file by hand, but that will take hours. Can someone please help me figure a way around this error in python?
Dropbox Data File set
*Update. Matlab, when trying to read one of the files using xlsread, says this:
Error using xlsread (line 251)
File C:\Users\zane\Documents\Research Projects\PneuFish Project\Data\Nov 28 2016 ATI
Data\ATI_Data_2016Y_11M_28D_16h_36m_01s.xls not in Microsoft Excel Format.
Thank you!
You can use the module xlrd.
import xlrd
import csv
def csv_from_excel():
wb = xlrd.open_workbook('your_workbook.xls')
sh = wb.sheet_by_name('Sheet1')
your_csv_file = open('your_csv_file.csv', 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in xrange(sh.nrows):
wr.writerow(sh.row_values(rownum))
your_csv_file.close()
Taken from This Post
This will convert from .xls to .csv, which is easily manipulated with Python.
You've said that the file is a text file, so don't tell Python that it's an Excel file. Just use Python's open and read it as text, then do whatever you want with it. open doesn't care what extension a file has.
I'm going to guess that the format is actually tab-delimited. From memory, earlier versions of Excel would read in tab-delimited text files with the .xls extension without complaint, whereas csv files would always bring up the text import wizard, so this was a common dodge if saving data intended for Excel from a program that didn't support writing real Excel files.
If you want the LabVIEW code to write real Excel files in future, the Write to Measurement File express VI has an option to write in xlsx format. I'm not sure which version of LabVIEW first introduced this but it's been there for a few years now.

Categories

Resources