Reading Excel files on Python - file untraceable - python

Here is my problem:
I have an assignement for uni where we're asked to make an analysis of some problem (not important).
We're given datas on excel files, and we are supposed to use python to code and solve our problem.
I've been through the internet to find out I had to install pip and to install the xlrd and openpyxl libraries (I wasn't sure which one).
I am hoping to be able to eventually use these files through Python and to manipulate the datas just thanks to some simple line commands, and I guess now I've fixed everything except one thing.
On Python Shell, I'm writting the following instructions:
import xlrd
import openpyxl
file_location = C:/Audrey/Desktop/CW/time.xlsx"
workbook = xlrd.open_workbook(file_location)
And then I got the line
"No such file or directory".
Though, I am sure, that I did not any mistake.
I have absolutely no clue what to do.
Did I forget a step ?
Can someone help ?

Forgot a quotation mark I believe:
file_location = "C:/Audrey/Desktop/CW/time.xlsx"

Related

Converting (ideally) doc to pdf with python or docx to pdf, but I get error

I am working in ios and with spyder (anaconda) trying the following code in order to convert docx files which are in a directory (folder_path):
from docx2pdf import convert
import os
no_pdfs = []
i=1
for filename in os.listdir(os.path.normcase(folder_path)):
filename = os.path.join(folder_path, filename)
try:
convert(filename, os.path.splitext(filename)[0]+'.pdf')
print(f"DONE - {i}: {os.path.basename(filename)}")
i += 1
except Exception:
no_pdfs.append(os.path.basename(filename))
print(no_pdfs)
I use try - except in my code because there is the .DS_Store that appears with ios and nothing happens.
If I brutally try convert() I get the error: ImportError: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html but I am not really able to understand what goes wrong.
An extra thing is that my initial files are not actually .docx but .doc and I would really like a piece of advice where I could convert doc to pdf or doc to docx to pdf.
Any help will be much appreciated!
If you haven't resolved this yet, you could try installing the Parallels Access from the Apple store on your ios but it sounds like you just have to update your packages (pip install --upgrade PackageName). The code you provided might be working, but the error is being flagged from those packages you mentioned. Also with docx2pdf, have you installed word on your device? From the creator of docx2pdf "Unfortunately, it requires Microsoft Office to be installed and thus only works on Windows and macOS. – Al Johri"
Also, An efficient way to convert document to pdf format is worth reading.

Unable to find nltk_data when adding binary and data files

I have a problem with exporting my python project. In this project, I am using the NLTK library, but when exporting it using the Pyinstaller tool the error mentioned in the title appears. I've been scouring a solution for weeks and I have not gotten an answer. I tried to use cx_freeze but other errors appear ... could anyone help me?
Tried use cx_freeze, and Pyapp.
'''Unable to find "/usr/nltk_data" when adding binary and data files.
I solved the problems editing the pyinstaller nltk-hook. After much research, I decided to go it alone in the code structure. I solved my problem by commenting on the lines:
datas=[]
'''for p in nltk.data.path:
datas.append((p, "nltk_data"))'''
hiddenimports = ["nltk.chunk.named_entity"]
What's more, you need to rename the file: pyi_rth__nltk.cpython-36.pyc to pyi_rth_nltk.cpython-36.pyc . This file have 1 more underline. Warning with the python version.
try making folder in your project /usr/nltk_data and there paste your nltk data.
do you have space in your username? avoid it.
nltk.download()
check the path there, or change it.

AttributeError: module 'pandas' has no attribute 'read_csv' Python3.5

I have been successfully using pandas.read_csv since long but suddenly it starts giving the error while I try to read a csv file
df = pd.read_csv('file.csv', encoding='utf-8')
The error is
AttributeError: module 'pandas' has no attribute 'read_csv'
I have tried to upgrade pandas but does not work. I tried to search and got this answer but when I search csv.py file in my pandas I didn't find any. So i tried to hover over the pandas.read_csv method which takes me to parsers.py file. But in that file there is no specific method named read_csv but it directed to another parser funtion like this
# parser.py (built-in file in pandas) file has this implementation
read_csv = _make_parser_function('read_csv', sep=',')
read_csv = Appender(_read_csv_doc)(read_csv)
I don't understand how should it start working again ? Any suggestions
I had the same problem when trying to run the following code in Jupyter/ipython.
import pandas as pd
df = pd.read_csv("weather_data.csv")
df
I realized I had a file named pandas.py. In fact, had two others named pandas1.py and pandas2.py as well. I changed them all and then it worked perfectly:) Lesson learned.
So I am writing an answer myself. I just noticed that I created a file random.py in my project which was creating a conflict with random.py in pandas package. Renaming my current file to something else worked for me :)
I faced the same problem and the solution that worked for me is as below.
Initially I installed the pandas and numpy with a regular user account. It installed library but there were few conflicts. So I uninstalled the libraries using pip uninstall package then installed them back as sudo account using sudo -H pip install package.
I hope it helps other people facing similar issue.
You literally just need to make sure that you have no ".py" files that have names of the same names of the packages. Like pandas.py, numpy.py etc..
Try print(pd)
Make sure you are getting this kind of output
<module 'pandas' from 'C:\\Users\\adarsh\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\pandas\\__init__.py'>
Otherwise, there may be another python file named pandas in your current working directory
For more click here
I also got the same problem but when I made a separate directory for the python file problem was solved. Make sure there is no other python file in your directory.
It is either the file named pandas.py or csv.py in the same directory or a name similar to one of the pandas files(which are many) in the same directory. I had a file named tokenize.py which it did not like. You can try checking in the pandas directory (search results in pandas) whether there are similar file names.
your project contains pandas.py possibly. rename it and try again.. would work.
Put your csv file in the directory or folder where your python project files are . This solution worked for me.
Make sure that, the library imported and the filename you are executing or the file you are having in the directory wont be having the samename.
Explainer Screenshot
Try to rename the Filename as Pandas1 or Pandas_Tutorial.py. It will solve the Problem.

How do I find out if/where xlsxwriter is on my system (for python)?

I am attempting to use the xlsxwriter package to write and image to an eXcel file, but I keep getting the 'No module name 'xlsxwriter' error on the import statement. I am on a closed network, so I am wondering also how I can find out where xlsxwriter is on my system as well as what other packages/extensions are available on my system. Thanks for reading my post - any help, ideas, or suggestions are greatly appreciated! Thanks, Don

How to call a python file, that needs to import packages?

I'm following a tutorial to call python code from a C++ program from the python docs.
Everything works just fine when trying to call the multiply example. Now if I add a line to the python source code importing a library, lets say openpyxl,
from openpyxl import load_workbook
I receive an error from python
ImportError: No module named openpyxl
I thought if I import a system library, I wouldn't have any problems, but I also get an error if I try to import datetime.
I don't have any error if I import the file from the python console. The openpyxl library is installed in my system.
So my question is: how to import python source code that needs to import packages?
EDIT: Ok, I forgot to mention something, I have not been completely honest with you guys, I'm sorry.
Trying to run the example I run into a problem: I couldn't make python found my multiply.py file, and the line PyImport_Import always return null.
My solution was to add the path in which I knew my python source was by using PySys_SetPath. The problem is that I just realized that this function doesn't append a new directory, it just overwrites the PYTHONPATH. So now python can find multiply.py, but absolutly anything else.
Of course I've deleted that line but now I have another question, why does python can't find my source if the file is just in the same directory of the C++ compiled program?
The I realized that my sys.path from my python console was a little different from the path showed in my embedded python: the first one had at the beginning of the list an empty string ''. I'm not a python expert, but when I add that line to my path I could import the multiply.py so it seems that was the reason I couldn't import modules that were located to relative to my executable was the missing of this empty path -but still don't know what it means-.
I have to thank to #paul-evans who give me the idea of adding the path to find my files.
This is what PYTHONPATH is for. You can set it as an environment variable containing a list module directories, or in the code itself something like:
import sys
sys.path.append("path/to/openpyxl/module")

Categories