pypdf errors - module object has no attribute number - python

here's the code I am using
import os
import decimal
from pyPdf import PdfFileReader
path = r"E:\python\Real Python\Real Python\Course materials\Chapter 8\Practice files"
inputFileName = os.path.join(path,"Pride and Prejudice.pdf")
inputFile = PdfFileReader(file(inputFileName,"rb"))
print "Number of pages:", inputFile.getNumPages()
print "Title:", inputFile.getDocumentInfo().title
Now, when I run this code I am getting an error:
'module' object has no attribute 'Number'
I took a screenshot of the whole output that I got
when I run the above code, with errors and everything.
so,please take a look and let me know what's wrong?
After I used import decimal in the code,
I got a few errors.
So, I took a screenshot of the whole thing and am attaching it here.

pyPdf is now renamed to pypdf and a couple of other classes / methods were renamed. I'm the maintainer of pypdf and PyPDF2.
Another change was that we dropped using decimal. Instead, we now use the built-in float. Hence this error will not occur in later versions of pypdf.

Related

Why does importing a file that contains a print() cause the output to be printed?

Consider the following Python files
file_one.py:
text = ("Sample text")
print(text)
file_two.py:
import file_one
Running python file_two.py gives the following output:
Sample Text
I was wondering why file_two automatically prints the output of file_one when it has been imported. I thought you may need to specifically call it to print out the text like print(file_one.text).
Similarly, in theory, is this any different from importing libraries such as random or pandas etc? As in, if they have a line that says print("hello"), hello will be printed automatically in the output of the module that imports it?
It’s because files are run when imported.
This might help
https://www.pythonmorsels.com/importing-module-runs-code/
When you import a file, everything at the top level of the file (that isn't part of a class or function) is run immediately.
That includes imports of other modules.

I edit an excel file and the following error appears UserWarning: Unknown extension is not supported and will be removed warn(msg) [duplicate]

I am trying to learn Python (day 2) and am hoping to practice with Excel books first as this is where I am comfortable/fluent.
Right off the bat I am having an error that I don't quit understand when running the below code:
import openpyxl
wb = openpyxl.load_workbook("/Users/Scott/Desktop/Workbook1.xlsx")
print(wb.sheetnames)
This does print my sheet names as requested, but it is followed by:
/Users/Scott/PycharmProjects/Excel/venv/lib/python3.7/site-packages/openpyxl/worksheet/_reader.py:293: UserWarning: Unknown extension is not supported and will be removed
warn(msg)
I have found other questions that point to slicers/conditional formatting etc, but that does not apply here. This is a book I just made and only added 3 sheets before saving. It has no data, no formatting, and the extension is valid. I have no add-ons installed on my excel either.
Any idea why why I am getting this error? How do I resolve?
Python: 3.7
openpyxl: 2.6
I had a similar issue. I developed an application which read and write Excel files. It woked well on my Windows computer, but then I tried to run it on a friends mac. It showed the same error. I could "fix" it by changing the configuration of the workbook, like this:
import openpyxl as op
wb = op.load_workbook(file, read_only=True, data_only=True)
But, as you can see, you can only read Excel files with this configuration. At the end, I realized that my friend didn't have Microsoft Office installed on his computer. Install it truly solved my problem.
This question was from a couple years ago but I'm encountering it now with openpyxl and require a fix, as the warning is confounding and misleading to my end users.
The warning from openpyxl comes via the stdlib warnings library, which can be suppressed.
import warnings
warnings.simplefilter("ignore")
That's the "hit it with a hammer" approach. More granular levels of warnings suppression can be found here: https://docs.python.org/3/library/warnings.html
This is exactly the problem I encountered just now..
And to my situation (not to everyone) I discovered that you just need to close your excel and rerun the code, very simple.
If this doesn't work, you can refer to other answers.
Thanks
Python - Openpyxl - "UserWarning: Unknown extension" issue
To understand the error, you need to know what's inside an XLSX file. The best way to take a look is to change the extension to zip and open that. Inside you will see a file called [Content_Types].xml and directories for the other content. If you check out the XML in Content_Types you will see a <Types ...> tag containing other tags like this:
<Default Extension="png" ContentType="image/png"/>
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
Note the "Extension" property. This is what the warning refers to. In the example above, my file included Extension="png" - the unknown extension.
For me, it was enough to specify read_only=True and the error went away eg:
wb = openpyxl.load_workbook(file, read_only=True)
I could also fix the issue by copying everything except the images to a new workbook and saving that. After checking, the xml in the new workbook no longer contained the png property.
Note, reading into pandas with pd.read_excel uses openpyxl and generates the same "Unknown extension" error but there is no way to pass through the read_only parameter. You can suppress the specific warning with:
import warnings
warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl')

How Do I Import a Local Python File With Relative Paths

I have been working at this for a while, but I have not been making a ton of progress, and I want to know how to import a python file into another, and with the help of a couple websites and debugging I have gotten to this code: import (__file__ + "\Assets\Minigames\minigame1"). I am getting this error in VSCode: Expected module namePylance Statements must be separated by newlines or semicolonsPylance

Attribute Error 'module' object has no attribute 'ascii_letters'

Why do I receive the error message in the title from the code below?
Edit: Cause I didn't pay attention to how I wrote "ascii". Thanks everyone
The code below works fine on my Iphone IDE but not on my Windows 7 (w/Notepad++ and Command Prompt). I checked the directory to see if any string.py files existed which I did not see any. I ran a search on my desktop and found 4 files named that, two of which said they were complied. I deleted the compiled files and left the other two. I'm a noob.
import string
import random
x = string.acsii_letters
y = random.choice(x)
print y * 5
It should be string.ascii_letters letters instead of string.acsii_letters. If that's a typo in code statement here only, then your guess must be right, there is another string module in your PYTHONPATH. Open python shell,
import string
print(string.__file__)
to ensure string is being imported from right path. If its not remove that path from PYTHONPATH.
In python 3 I found that using the string.ascii_letters works as string.letters results in an AttributeError.
You have a typo. It should be string.ascii_letters or string.letters. You can look at the attributes of the string module with dir(string) and see what you can access.
i had the same issue the reason was that the name of the file is the same of the module name . so just rename your file the module will work well

does anybody knows pickle with commandline?

I am trying to make a dictionary with pickle by using command line but while getting input from command line I am getting "module object has no attribute load " ?
Here's my code:
import pickle
import sys
dfile = open ("new.dat","w")
print "get argument"
lifesize=(sys.argv[1])
print "get another argument"
two=sys.argv[2]
print "last argument"
three=sys.argv[3]
z={lifesize:[two,three]}
pickle.dump(z,dfile)
dfile.close()
ifile=open("new.dat")
d1= pickle.load(ifile)
and save above as newdocument
cmd:python newdocument.py
I also tried to do a dictionary for every lifesize and save them as new.dat and get them..
need really help .?thank you
The code you posted is perfectly fine, please check your version of Python, and possibly update/reinstall.
The Python interpreter complains that pickle.load() doesn't exist, while it certainly does: http://docs.python.org/library/pickle.html#pickle.load

Categories