I use the pywin32 library since one week to parse visio files to extract shapes and their content. I ran my scripts several times but since yesterday when I execute them I have this error :
File "C:\Program Files\Python39\lib\site-packages\win32com\client\__init__.py", line 580, in __getattr__ raise AttributeError( AttributeError: '<win32com.gen_py.Microsoft Visio 16.0 Type Library.IVDocument instance at 0x1943434388768>' object has no attribute 'pages'
Here is the part of my script which generate the error :
import glob
import os
import win32com.client as w32
path = r"C\Users\..."
all_files = glob.glob(path + "/*.vsd")
visio = w32.Dispatch("visio.Application")
for filename in all_files:
print(filename)
vdoc = visio.Documents.Open(filename)
page = vdoc.pages(1) <-- the problematic line
shps = page.Shapes
I make a list of visio files and after I open them in the for loop I read the first page (they have all one page). The first visio open as well but after I have the error.
I tried to uninstall et reinstall pywin32, I worked in another repertory, change my import name... I tried on another PC and pywin works as well.
I really don't understand why python rise this error now, above all I don't touch this line .
Have you some ideas to resolve this problem ?
I don't understand how it works but capitalizing all commands like .Pages/.Type/.Text/.Shapes/.Names because before the commands uncapitatlized .pages/.type/.text/.shapes/.names worked... It's very weird if somebody have an explanation I take it.
Related
I am learning Python through 'Automate the Boring Stuff With Python' First Edition. In chapter 12, pg 267, we are supposed to open a file called example.xlsx.
The author's code reads:
import openpyxl
wb = openpyxl.load_workbook('example.xlsx')
type(wb)
However, when I try to open this file, I get the following error (this is the last line of the error):
FileNotFoundError: [Errno 2] No such file or directory: 'example.xlsx'
I know this file exists, because I downloaded it myself and am looking at it right now.
I have tried moving it to the current location in which Python 3.8 is, I have tried saving it with my Automate the Boring Stuff files that I've been working on the desktop, and I have tried saving it in every conceivable location on my machine, but I continue getting this same message.
I have imported openpyxl without error, but when I enter the line
wb = openpyxl.load_workbook('example.xlsx')
I have entered the entire pathway for the example.xlsx in the parenthesis, and I continue to get the same error.
What am I doing wrong? How am I supposed to open an Excel workbook?
I still don't understand how I am doing wrong, but this one is incredibly infuriating, and I feel incredibly stupid, because it must be something simple.
Any insight/help is greatly appreciated.
Your error is unambigous — your file in a supposed directory don't exist. Believe me.
For Python is irrelevant, whether you see it. Python itself must see it.
Specify the full path, using forward slashes, for example:
wb = openpyxl.load_workbook('C:/users/John/example.xlsx')
Or find out your real current (working) directory — and not the one supposed by you — with commands
import os
print(os.getcwd())
then move your example.xlsx to it, and then use only the name of your file
wb = openpyxl.load_workbook('example.xlsx')
You may also verify its existence with commands — use copy/paste from your code to avoid typos in the file name / path
import os.path
print(os.path.exists('example.xlsx')) # True, if Python sees it
or
import os.path
print(os.path.exists('C:/users/John/example.xlsx')) # True, if exists
to be sure that I'm right, i.e. that the error is not in the function openpyxl.load_workbook() itself, but in its parameter (the path to the file) provided by you.
I notice that the extension of the example file is not the same as described in the book, is example.csv. I was facing the same frustration as you
I am in my project folder call "project". I have two neural network h5 file, one in "project/my_folder/my_model_1.h5", I also copy it to folder "project/my_model_2.h5". So I open my Jupyter Notebook which is working at "project" folder.
import h5py
f = h5py.File("my_model_2.h5") # has NO Issue
but
f = h5py.File("my_folder/my_model_1.h5") # OSError
It says OSError: Unable to open file (unable to open file: name = 'my_folder/my_model_1.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)
Interestingly, I only have this issue when I do the same thing on my Mac, but I don't encounter any issue in Linux machine.
Please let me know if you know how to fix this. Thank you in advance.
So it looks like some hidden invalid character incidentally got copied when I simply copy and paste the file path from Mac folder system. Take a look at the code in the screen.
The Line 92 is the path name I directly copy and paste from Mac folder.
The Line 93 is the path I literally type with every single letter, then there is no error and .h5 file is loaded properly. It's a kinda of similar issue that has been spotted by someone at this link: Invalid character in identifier
I simply copy the error code to Pycharm, and the unwelcome character got busted.
So solution, for Mac user, be careful of of just simply copying the text from folder system, if something obviously weird, try type every letter into the text editor.
Specifying the absolute path using the os worked in windows
file_name = os.path.dirname(__file__) +'\\my_folder\\my_model_1.h5'
f = h5py.File(file_name)
dont forget to import os though
I am using openpyxl python excel reader and writer in my Ubuntu Server
When I use following command
from openpyxl import load_workbook,Workbook
book = load_workbook(filename='/var/www/test.xlsx')
throwing error as
for _name in node:
TypeError: 'NoneType' object is not iterable
but everything fine in local system. Any one know why this is happening and how to solve this?
Update
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/excel.py", line 202, in load_workbook
parsed_styles = read_style_table(archive)
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/style.py", line 181, in read_style_table
p.parse()
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/style.py", line 53, in parse
self.parse_named_styles()
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/style.py", line 109, in parse_named_styles
names = self._parse_style_names()
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/style.py", line 128, in _parse_style_names
for _name in node:
TypeError: 'NoneType' object is not iterable
It looks like openpyxl isn't able to understand your list of named styles. You should first extract the .xlsx file (it's but a .zip archive with a different ending), and look for the XML file that defines styles (it's xl/styles.xml). Verify it's well-formed XML, and not corrupted. You can do that with one of the many online XML checkers.
If that's not the culprit, you might just try to rely on openpyxl's "benevolence", delete the styles.xml, re-compress to a .zip, rename to .xlsx, and try again.
I had the same problem, and it appeared that the path was incorrect. Open the file with Excel, and copy-paste the path from the "File" tab.
In many cases it has to do with malformed style sheets, as previous posters have already mentioned. I have a few .xlsx files generated by Oracle SQLDeveloper.
When I want to parse them with openpyxl I need to open and then save them in Excel. Otherwise I get the same error as you did.
I did not compare the style sheets (before and after re-saving the file), so I can't tell you what's being fixed by saving the file in Excel. It just works in this particular case, but it may work in yours as well.
This is a bug in openpyxml version 2.3, see: https://bitbucket.org/openpyxl/openpyxl/issues/544, it was fixed in 2.3.1.
Problem can be easily solved by upgrading to the newest version, run:
pip install openpyxl -I
This way pip ignores your current library version and gets the latest one.
I ran into this problem today on Windows.
It turned out it was because while saving the file, under "Save as type: " I selected Strict Open XML Spreadsheet (*.xlsx).
It worked when I saved it as Excel Workbook (*.xlsx)
I had assumed all .xlsx files were the same, this is apparently not the case.
I have problem in opening FITS file in Python. I get following error-message:
File "G:\Anaconda\lib\site-packages\pyfits\file.py", line 416, in _open_filelike % self.mode)
IOError: File-like object does not have a 'write' method, required for mode 'ostream'
at hdulist = pft.open(path) line (I did import pyfits as pft).
I checked the path twice - it's correct.
I'm not able to find any reference to this error in context of using PyFITS and I will be gratefull for any help.
UPDATE:
I missed some details and I'm sorry for it.
First of all: I'm using PyFITS 3.3 under Anaconda distribution for Windows (Windows XP 32-bit).
Code of whole widget you can find at this link:
FileView
In a short - I'm making simple explorer for filesystem, just to let user navigate to folder with FITS files and read it from folder. All project is under PyQT4.
Obviously your path is not a subclass of basestring (I suppose you use Python 2.7) as it is expected by PyFITS. In fact path is a QString instance and you have to convert to unicode first.
So replace your line
hdulist = pft.open(path)
with
hdulist = pft.open(unicode(path.toUtf8(), encoding="UTF-8"))
I have a rather simple program that writes HTML code ready for use.
It works fine, except that if one were to run the program from the Python command line, as is the default, the HTML file that is created is created where python.exe is, not where the program I wrote is. And that's a problem.
Do you know a way of getting the .write() function to write a file to a specific location on the disc (e.g. C:\Users\User\Desktop)?
Extra cool-points if you know how to open a file browser window.
The first problem is probably that you are not including the full path when you open the file for writing. For details on opening a web browser, read this fine manual.
import os
target_dir = r"C:\full\path\to\where\you\want\it"
fullname = os.path.join(target_dir,filename)
with open(fullname,"w") as f:
f.write("<html>....</html>")
import webbrowser
url = "file://"+fullname.replace("\\","/")
webbrowser.open(url,True,True)
BTW: the code is the same in python 2.6.
I'll admit I don't know Python 3, so I may be wrong, but in Python 2, you can just check the __file__ variable in your module to get the name of the file it was loaded from. Just create your file in that same directory (preferably using os.path.dirname and os.path.join to remain platform-independent).