Convert TDMS-File to XLSX - python

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.

Related

How to read the files of Azure file share as csv that is pandas dataframe

I have few csv files in my Azure File share which I am accessing as text by following the code:
from azure.storage.file import FileService
storageAccount='...'
accountKey='...'
file_service = FileService(account_name=storageAccount, account_key=accountKey)
share_name = '...'
directory_name = '...'
file_name = 'Name.csv'
file = file_service.get_file_to_text(share_name, directory_name, file_name)
print(file.content)
The contents of the csv files are being displayed but I need to pass them as dataframe which I am not able to do. Can anyone please tell me how to read the file.content as pandas dataframe?
After reproducing from my end, I could able to read a csv file into dataframe from the contents of the file following the below code.
generator = file_service.list_directories_and_files('fileshare/')
for file_or_dir in generator:
print(file_or_dir.name)
file=file_service.get_file_to_text('fileshare','',file_or_dir.name)
df = pd.read_csv(StringIO(file.content), sep=',')
print(df)
RESULTS:

Pandas gives an unordered csv file

What can I do to make this (1 Pic):
look like this one with pandas (2 Pic):
Here's the code I used to make the csv file in the 1 Picture
import pandas as pd
import os
all_months_data = pd.DataFrame()
files = [file for file in os.listdir('Sales_Data/')]
for file in files:
df = pd.read_csv('Sales_Data/' + file)
all_months_data = pd.concat([all_months_data, df])
all_months_data.to_csv('all_data.csv')
I just figured the problem and it was Exel itself that have read my csv file as a text.
I did this and it worked:
Open Excel
Go to 'Data' tab
Select 'From Text/CSV' and select the .CSV file you want to import.
Click 'Import' and you're done!

Excel removes my VBA project from .bin file

I want to add VBA project to my excel file I'm creating using 'xlsxwriter' library. In their example https://xlsxwriter.readthedocs.io/working_with_macros.html they extract vba project from another excel file and add it to the current. I want to add an already existing .bin file to my excel file. The problem is that excel removes this file. Here is my python code
from xlsxwriter import *
import os
workbook = Workbook('ShipBattle.xlsm')
menuws = workbook.add_worksheet('Menu')
gamews = workbook.add_worksheet('Game')
workbook.set_vba_name('MyWorkbook')
menuws.set_vba_name('Sheet1')
gamews.set_vba_name('Sheet2')
workbook.add_vba_project('vbaProject.bin')
gamews.insert_button('F10', {'macro' : 'Test', 'caption' : 'End Turn', 'width' : 80, 'height' : 30})
workbook.close()
and here is my vba code from .bin file
Sub Test()
MsgBox "Hello, world!"
End Sub
Here is the window:
pic of removing my vbaProject.bin

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.

Iterating through excel files and capturing a specific cell value in each file

I have a directory of participation forms (as excel files) from clients, and I want to write a script that will grab all of the relevant cells from the participation form and write them to an excel doc where each client is on its own row. When I try and iterate through the directory using the following code:
import os
import xlrd
import xlwt
from xlrd import open_workbook
from xlwt import easyxf
import pandas as pd
from pandas import np
import csv
for i in os.listdir("filepath"):
book=xlrd.open_workbook("filepath",i)
print book
sheet=book.sheet_by_index(0)
a1=sheet.cell_value(rowx=8, colx=3)
print a1
I get the error: IOError: [Errno 13] Permission denied: 'filepath'
EDIT Here is the Full Traceback after making edits suggested by Steven Rumbalski:
Traceback (most recent call last):
File "C:\Users\Me\Desktop\participation_form.py", line 11, in <module>
book=xlrd.open_workbook(("Y:/Directory1/Directory2/Signup/", i))
File "c:\python27\lib\site-packages\xlrd\__init__.py", line 394, in open_workbook
f = open(filename, "rb")
TypeError: coercing to Unicode: need string or buffer, tuple found
xlrd.open_workbook expects its first argument to be a full path to a file. You are trying to open the folder and not the file. You need to join the filepath and the filename. Do
book = xlrd.open_workbook(os.path.join("filepath", i))
You also my want to guard against trying to open things that are not excel files. You could add this as the first line of your loop:
if not i.endswith((".xls", ".xlsx")): continue
You can simplify all of this with the glob module and the .read_excel() method in pandas (which you already seem to be importing). The following iterates over all the files in some directory that match "*.xlsx", parses them into data frames, and prints out the contents of the appropriate cell.
from glob import glob
for f in glob("/my/path/to/files/*.xlsx"):
print pd.read_excel(f).ix[8,3]

Categories

Resources