Python - Opening a Password Protected CSV to read/write - python

I have been using python for only about two months so I am still quite new to coding.
Recently, in work, I wrote a code which opens an existing CSV file, performs a few operations and spits out a new CSV file. That bit I am happy with.
But what I want to know is what can I do in terms of securing the document and still running the code to open it? For example, I want to password protect this CSV file but want to prompt the user for the password which will be the only way to open/read the file.
Can anyone point me in the right direction please?

The CSV file is simply a formatted text (.txt) file. So to protect the file there are a few approaches.
Save CSV file then change permissions on it using OS commands - Password Protecting Excel file using Python
zip the csv file with password. Unfortunately, "The builtin zipfile module does not support writing password-encrypted files (only reading). Either you could use pyminizip. Refer to Create password protected zip file Python

Related

Python: Edit a xlsx file open by another user

I looked at google for a solution based on python, but did not find any...
My python script is trying to edit an xlsx that might be opened by another user from MS excel.
If I try to overwrite the .xlsx file or the ~$*.xlsx file, I get a winError 32:
'process cannot access the file because it is being used by another process'
My problem is that users around me use MS excel to look at this output... And MS excel always lock the files that are open, by default.
It there a way to 'steal' the access from the other users. (As they are not editing it anyway).
I cannot not change the user permission (I think) as I am not admin of the files.
I am using windows 10.
Thanks for your advices.
cheers.
There really is no way around this - it is Excel preventing any other process on the system from obtaining write access.
If it were running on the same machine, you could consider connecting to the running Excel instance and getting it to close and reopen the document after opening it for writing yourself, but in your example it would likely be opened by someone on another machine.
The only solution here is to instruct your users to open the worksheet as read-only, which is an option every version of Excel allows, in which case you might be able to open it for writing. Whether that will allow you to update it while they are looking at it, is doubtful - you likely may want to look into connecting to an Excel sheet on OneDrive or SharePoint (or Teams etc. that use SharePoint as a back-end).

How do I open/convert .pkz files?

A python package that I'm using has data stored under a single file with a .pkz extension. How would I unzip (?) this file to view the format of data within?
Looks like what you are referencing is just a one-off file format used in sample data in scikit-learn. The .pkz is just a compressed version of a Python pickle file which usually has the extension .pkl.
Specifically you can see this in one of their sample files here along with the fact they are using the zlib_codec. To open it, you can go in reverse or try uncompressing from the command line.
Before attempting to open an PKZ file, you'll need to determine what kind of file you are dealing with and whether it is even possible to open or view the file format.
Files which are given the .PKZ extension are known as Winoncd Images Mask files, however other file types may also use this extension. If you are aware of any additional file formats that use the PKZ extension, please let us know.
How to open a PKZ file:
The best way to open an PKZ file is to simply double-click it and let the default assoisated application open the file. If you are unable to open the file this way, it may be because you do not have the correct application associated with the extension to view or edit the PKZ file.
If you can do it, great, you have a program installed that can do it, lets say that program is called pkzexecutor.exe, with python, you just have to do:
import subprocess
import os
path_to_notepad = 'C:\\Windows\\System32\\pkzexecutor.exe'
path_to_file = 'C:\\Users\\Desktop\\yourfile.pkz'
subprocess.call([path_to_notepad, path_to_file])
From the source code for fetch_olivetti_faces, the file appears to be downloaded from http://cs.nyu.edu/~roweis/data/ and originally has a .mat file extension, meaning it is actually a MATLAB file. If you have access to MATLAB or another program which can read those files, try opening it from there with the original file extension and see what that gives you.
(If you want to try opening this file in Python itself, then perhaps give this question a look: Read .mat files in Python )

Reading excel file from zip archive using python and openpyxl

I have a password protected zip archive containing some excel spreadsheets with some confidential data. I'd like to take the password from the user, open the zip, and analyze the spreadsheet inside, without actually extracting the excel file. Is it possible to construct openpyxl's workbook from the zip entry directly and do some analysis on the data in the workbook? I am trying to avoid extracting the excel to file system to avoid potential security problems (e.g. undeleted temp files).
Is this possible to do? Is it possible, for example, to treat the zip archive as some pseudo file system?
Thanks in advance!

Reading from a CSV file while it is being written to

So before I start I know this is not the proper way to go about doing this but this is the only method that I have for accessing the data I need on the fly.
I have a system which is writing telemetry data to a .csv file while it is running. I need to see some of this data while it is being written but it is not being broadcast in a manner which allows me to do this.
Question: How do I read from a CSV file which is being written to safely.
Typically I would open the file and look at the values but I am hoping to be able to write a python script which is able to examine the csv for me and report the most recent values written without compromising the systems ability to write to the file.
I have absolutely NO access to the system or the manner in which it is writing to the CSV I am only able to see that the CSV file is being updated as the system runs.
Again I know this is NOT the right way to do this but any help you could provide would be extremely helpful.
This is mostly being run in a Windows environment
You can do something like:
tailf csv_file | python your_script.py
and read from sys.stdin

Working with Password Protected Excel Sheets in Python on Linux

The problem is pretty simple. Every week I receive a bunch of password protected excel files. I have to parse through them and write certain parts to a new file using Python. I am given the password for the files.
This was simple to handle when this was being done on Windows and I could just import win32com and use client.Dispatch. But we are now moving all our code to linux so no more win32com for me.
Is there a way to open and read data from a password protected excel sheet in python on linux?
I have been searching for simple way to open a password protected excel file but no luck. I also tried finding a way to just remove the password protection so I can use xlrd like I would on a file that is not password protected but no luck going that route either.
Any help would be most appreciated.
with libreoffice and unoconv
unoconv --password='p4ssw0rd' -f csv protectedFile.xls
and then parse the csv file. Or export to another xls if you need the formatting or want to torture yourself
N.B. Edited after accepted. (--password is the correct switch, not -p, as noted by #enharmonic)
I've recently had an easier time using xlsxunpass
java -jar ./xlsxunpass.jar protected.xlsx unprotected.xlsx 'p4ssw0rd'

Categories