python function to manipulate macro enabled excel sheets - python

The code below is supposed to navigate through a macro enabled file delete the rows and save the file again. all the steps are running and the file is saved. However, when I try to open the file I get this error:
"Excel cannot open the file "Matrix_2022xlsm" because the file format or the file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
warnings.simplefilter(action='ignore', category=UserWarning)
path = "path/Matrix_2022.xlsm" #file path
book = xl.load_workbook(path) #load file
sheet = book.worksheets[2]
print("Maximum rows before moving:", sheet.max_row)
sheet.delete_rows(1, sheet.max_row-1) # specify from which row to which row we are deleting
print("Maximum rows after removing:", sheet.max_row)
# save the file to the path
path = "path/Matrix_2022.xlsm"
book.save(path)

Related

Convert TDMS-File to XLSX

I would like to convert a folder full of TDMS files 1:1 to XLSX.
Since important is that the Excel file has the same tabs as the TDMS file and the same file name.
I get the tabs read and the file names, but I don't know how to create new Excel files with the same names and content as the TDMS. Thats what i have tried so far:
from nptdms import TdmsFile
from nptdms import tdms
import os,glob
#Namen aller TDMS-Dateien in einem Ordner
file_names=glob.glob('*.tdms')
for file in glob.glob("*.tdms"):
tdms_file = TdmsFile.read(file)
tdms_groups = tdms_file.groups()
print(tdms_groups)
Now i found out, how to save each TDMS-File as XLSX,
import os, xlsxwriter,glob
import numpy as np
import pandas as pd
from nptdms import TdmsFile
from nptdms import tdms
file_names=glob.glob('*.tdms')
# Read the files back into a dataframe
dataframe_list = []
for file in glob.glob("*.tdms"):
tdms_file = TdmsFile.read(file)
df = tdms_file['Line 1'].as_dataframe()
dataframe_list.append(df)
file = file.replace(".tdms", "")
df.to_excel(str(file)+".xlsx")
But the problem is i have to know the path name ( Line 1 in this case).
I want to find out the path or group names, to save the XLSX File with alle tabs and the same name as in the original TDMS-File.
So can someone tell me how to read the individual tab names before opening the file and then create an XLSX file with the same number of tabs, the same tab name and content?
Edit:
When i use the command tdms_file.groups() i´ll get the following output:
[<TdmsGroup with path /'Line 1'>, <TdmsGroup with path /'Current_Line 1'>]
, but i can´t just get the tab names only ( "Line 1" and "Current Line 1"). After that i want to create an XLSX-File with the tab "Line 1" and the tab "Current Line 1" with the same content.

Downloaded Share Point Excel Not Opening with Open

I am re-framing an existing question for simplicity. I have the following code to download Excel files from a company Share Point site.
import requests
import pandas as pd
def download_file(url):
filename = url.split('/')[-1]
r = requests.get(url)
with open(filename, 'wb') as output_file:
output_file.write(r.content)
df = pd.read_excel(r'O:\Procurement Planning\QA\VSAF_test_macro.xlsm')
df['Name'] = 'share_point_file_path_documentName' #i'm appending the sp file path to the document name
file = df['Name'] #I only need the file path column, I don't need the rest of the dataframe
# for loop for download
for url in file:
download_file(url)
The downloads happen and I don't get any errors in Python, however when I try to open them I get an error from Excel saying Excel cannot open the file because the file format or extension is not valid. If I print the link in Jupyter Notebooks it does open correctly, the issue appears to be with the download.
Check r.status_code. This must be 200 or you have the wrong url or no permission.
Open the downloaded file in a text editor. It might be a HTML file (Office Online)
If the URL contains a web=1 query parameter, remove it or replace it by web=0.

how to save the json file as csv file by the name of the json file with .csv extension

I have a set of 200 JSON file in a folder, i have written a code to take each file from the folder and then convert the JSON files to data-frame do the necessary step and finally save the data-frame as a csv file,the problem i face is to save the csv file, i wanted to save the file as csv in the name of the JSON file.
Since i am taking the folder and processing the files one by one how can i do that
i tried this form
df.to_csv(filename)
but i have to give the filename
Assuming you are not accessing the file by manually calling its name:
with open('whatever.json', 'rb') as file
And using something like glob. I would do something like this:
import os
#File = to whatever variable name you have assigned to the opened json file
filename = os.path.basename(File.name)
filename = filename.split('.')[0]
filename += '.csv'
as requested:
with open(filename, 'w') as file:
file.write(your csv data)
file.close()

File upload at web2py

I am using the web2py framework.
I have uploaded txt a file via SQLFORM and the file is stored in the "upload folder", now I need to read this txt file from the controller, what is the file path I should use in the function defined in the default.py ?
def readthefile(uploaded_file):
file = open(uploaded_file, "rb")
file.read()
....
You can do join of application directory and upload folder to build path to file.
Do something like this:
import os
filepath = os.path.join(request.folder, 'uploads', uploaded_file_name)
file = open(filepath, "rb")
request.folder: the application directory. For example if the
application is "welcome", request.folder is set to the absolute path
"/path/to/welcome". In your programs, you should always use this
variable and the os.path.join function to build paths to the files you
need to access.
Read request.folder
The transformed name of the uploaded file is stored in the upload field of your database table, so you need a way to query the specific record that was inserted via the SQLFORM submission in order to get the name of the stored file. Here is how it would look assuming you know the record ID:
stored_filename = db.mytable(record_id).my_upload_field
original_filename, stream = db.mytable.my_upload_field.retrieve(stored_filename)
stream.read()
When you pass a filename to the .retrieve method of an upload field, it will return a tuple containing the original filename as well as the open file object (called stream in the code above).

Downloading a file with a wildcard from Sharepoint URL with Python

I'm not sure if this can be done or not but I thought I'd ask!
I'm running a windows 10 PC using Python 2.7. I'm wanting to download a file form sharepoint to a folder on my C: drive.
OpenPath = "https://office.test.com/sites/Rollers/New improved files/"
OpenFile = "ABC UK.xlsb"
The downside is the file is uploaded externally & due to human error it can be saved as a .xlsx or ABC_UK. Therefor I only want to use the first 3 characters with a wildcard (ABC*) to open that file. Thankfully the first 3 Characters are unique & there only be one file in the path that should match.
to find the file in your dir:
import os, requests, fnmatch
#loop over dir
for filename in os.listdir(OpenPath):
#find the file
if fnmatch.fnmatch(filename, 'ABC_UK*'):
#download the file
# open file handler
with open('C:\dwnlfile.xls', 'wb') as fh:
#try to get it
result = requests.get(OpenPath+filename)
#check u got it
if not result.ok:
print result.reason # or text
exit(1)
#save it
fh.write(result.content)
print 'got it and saved'

Categories

Resources