AttributeError: 'function' object as no attribute 'seek' with ebooklib package - python

This is my current code
import ebooklib
from ebooklib import epub
class EpubToTextClass:
def __init__(self, dirToEpub=""):
self.dirToEpub = dirToEpub
def setDirToEpub(self, dirToEpub):
self.dirToEpub = dirToEpub
def convertEpubToText(self):
book = epub.read_epub(self.convertEpubToText)
items = list(book.get_items_of_type(ebooklib.ITEM_DOCUMENT))
text = ""
for i in items:
text = text + i
return text
ebpub = EpubToTextClass()
ebpub.setDirToEpub("-Some-EPUB.epub")
print(ebpub.convertEpubToText())
The goal of this part of the code is to convert a epub book to text, via the ebooklib but this error keeps popping up, and I am not completely sure what the error is about.
If you would like to see my whole error log, here it is:
Traceback (most recent call last):
File "epubToText.py", line 22, in <module>
print(ebpub.convertEpubToText())
File "epubToText.py", line 13, in convertEpubToText
book = epub.read_epub(self.convertEpubToText)
File "/home/jeff/.local/lib/python3.8/site-packages/ebooklib/epub.py", line 1739, in read_epub
book = reader.load()
File "/home/jeff/.local/lib/python3.8/site-packages/ebooklib/epub.py", line 1397, in load
self._load()
File "/home/jeff/.local/lib/python3.8/site-packages/ebooklib/epub.py", line 1686, in _load
self.zf = zipfile.ZipFile(self.file_name, 'r', compression=zipfile.ZIP_DEFLATED, allowZip64=True)
File "/usr/lib/python3.8/zipfile.py", line 1269, in __init__
self._RealGetContents()
File "/usr/lib/python3.8/zipfile.py", line 1332, in _RealGetContents
endrec = _EndRecData(fp)
File "/usr/lib/python3.8/zipfile.py", line 264, in _EndRecData
fpin.seek(0, 2)
AttributeError: 'function' object has no attribute 'seek'

I figured it out later on.
Which was to change this:
book = epub.read_epub(self.convertEpubToText)
to
book = epub.read_epub(self.dirToEpub)

Related

I'm getting python google translate "nontype" error

My code is bellow and I am getting "'NoneType' object has no attribute 'group'" error what is the reason? (And sorry for my question form I am a newbie at this)
from PIL import Image
import pytesseract
import googletrans
from googletrans import Translator
translator = Translator()
pytesseract.pytesseract.tesseract_cmd = r"C:\\Program Files\\Tesseract-OCR\\tesseract.exe"
with open("emre.txt", mode = "w+") as f:
f.write(pytesseract.image_to_string("01-361.jpg"))
s = open("emre.txt", "r")
if s.mode == "r":
ceviri = s.read()
print(ceviri)
result = translator.translate(ceviri, src= "en", dest="tr")
with open("çeviri.txt", "w") as k:
k.write(result.text)
And my error is =
Traceback (most recent call last):
File "c:\Users\usr\amazon_py\çeviri.py", line 20, in <module>
result = translator.translate(ceviri, src= "en", dest="tr")
File "C:\Users\usr\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\googletrans\client.py", line 182, in translate
data = self._translate(text, dest, src, kwargs)
File "C:\Users\usr\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\googletrans\client.py", line 78, in _translate
token = self.token_acquirer.do(text)
File "C:\Users\usr\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\googletrans\gtoken.py", line 194, in do
self._update()
File "C:\Users\usr\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\googletrans\gtoken.py", line 62, in _update
code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'
after the google translate downgrade error =
File "c:\Users\usr\amazon_py\çeviri.py", line 20, in <module>
emre = translator.translate(ceviri, src= "en", dest="tr")
File "C:\Users\usr\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\googletrans\client.py", line 219, in translate
parsed = json.loads(data[0][2])
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 339, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not NoneType

TypeError: expected <class 'openpyxl.styles.fills.Fill'>

I`m trying to download and then open excel file (report) generated by marketplace with openpyxl.
import requests
import config
import openpyxl
link = 'https://api.telegram.org/file/bot' + config.TOKEN + '/documents/file_66.xlsx'
def save_open(link):
filename = link.split('/')[-1]
r = requests.get(link)
with open(filename, 'wb') as new_file:
new_file.write(r.content)
wb = openpyxl.open ('file_66.xlsx')
ws = wb.active
cell = ws['B2'].value
print (cell)
save_open(link)
After running this code I got the above:
Traceback (most recent call last):
File "C:\Python 3.9\lib\site-packages\openpyxl\descriptors\base.py", line 55, in _convert
value = expected_type(value)
TypeError: Fill() takes no arguments
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Home\Documents\myPython\bot_WB\main.py", line 20, in <module>
save_open(link)
File "C:\Users\Home\Documents\myPython\bot_WB\main.py", line 14, in save_open
wb = openpyxl.open ('file_66.xlsx')
File "C:\Python 3.9\lib\site-packages\openpyxl\reader\excel.py", line 317, in load_workbook
reader.read()
File "C:\Python 3.9\lib\site-packages\openpyxl\reader\excel.py", line 281, in read
apply_stylesheet(self.archive, self.wb)
File "C:\Python 3.9\lib\site-packages\openpyxl\styles\stylesheet.py", line 198, in apply_stylesheet
stylesheet = Stylesheet.from_tree(node)
File "C:\Python 3.9\lib\site-packages\openpyxl\styles\stylesheet.py", line 103, in from_tree
return super(Stylesheet, cls).from_tree(node)
File "C:\Python 3.9\lib\site-packages\openpyxl\descriptors\serialisable.py", line 103, in from_tree
return cls(**attrib)
File "C:\Python 3.9\lib\site-packages\openpyxl\styles\stylesheet.py", line 74, in __init__
self.fills = fills
File "C:\Python 3.9\lib\site-packages\openpyxl\descriptors\sequence.py", line 26, in __set__
seq = [_convert(self.expected_type, value) for value in seq]
File "C:\Python 3.9\lib\site-packages\openpyxl\descriptors\sequence.py", line 26, in <listcomp>
seq = [_convert(self.expected_type, value) for value in seq]
File "C:\Python 3.9\lib\site-packages\openpyxl\descriptors\base.py", line 57, in _convert
raise TypeError('expected ' + str(expected_type))
TypeError: expected <class 'openpyxl.styles.fills.Fill'>
[Finished in 1.6s]
If you run file properties/details you can see that this file was generated by "Go Exelize" (author: xuri). To run this file you need to separate code in two parts. First: download file. Then you need to manually open it with MS Excel, save file and close it (after this "Go Excelize" switch to "Microsoft Excel"). And only after that you can run the second part of the code correctly with no errors. Can anyone help me to handle this problem?
I had the same problem, "TypeError('expected ' + str(expected_type))", using pandas.read_excel, which uses openpyxl. If I open the file, save and close it, it will work with both, pandas and openpyxl.
Upon further attempts I could open the file using the "read_only=True" in openpyxl, but while iterating over the rows I would still get the error, but only when all the rows ended, in the end of the file.
I belive it could be something in the EOF (end of file) and openpyxl don't have ways of treating it.
Here is the code that I used to test and worked for me:
import openpyxl
wb = openpyxl.load_workbook(my_file_name, read_only=True)
ws = wb.worksheets[0]
lis = []
try:
for row in ws.iter_rows():
lis.append([cell.value for cell in row])
except TypeError:
print('Skip error in EOF')
Used openpyxl==3.0.10

AttributeError: 'NoneType' object has no attribute 'group' using googletrans and pyjokes

joke = pyjokes.get_joke() ## Get a joke.
translator = Translator() ## Initalize a Translator object.
print( translator.translate(joke, dest='hi').text)
Traceback (most recent call last): File "A.I.py", line 206, in <module>
print( translator.translate(joke, dest='hi').text) File "C:\Users\NEERAJ KUSHWAH\AppData\Local\Programs\Python\Python38\lib\site-packages\googletrans\client.py", line 182, in translate
data = self._translate(text, dest, src, kwargs) File "C:\Users\NEERAJ KUSHWAH\AppData\Local\Programs\Python\Python38\lib\site-packages\googletrans\client.py", line 78, in _translate
token = self.token_acquirer.do(text) File "C:\Users\NEERAJ KUSHWAH\AppData\Local\Programs\Python\Python38\lib\site-packages\googletrans\gtoken.py", line 194, in do
self._update() File "C:\Users\NEERAJ KUSHWAH\AppData\Local\Programs\Python\Python38\lib\site-packages\googletrans\gtoken.py", line 62, in _update
code = self.RE_TKK.search(r.text).group(1).replace('var ', '') AttributeError: 'NoneType' object has no attribute 'group'

Python - Error in recursion doesn't return string variable

I am building a Python Application that searches files with a given path and returns them.
This is the code:
def search(path):
name = 'filename'
files = os.listdir(path)
for filename in files:
fullname = os.path.join(path, filename)
if name in fullname and 'rat' not in fullname:
print(colored('[*] Found: '+fullname+' [*]','blue'))
return ''+fullname
elif os.path.isdir(fullname):
return search(fullname)
When i run this piece of code:
path = search('output/host/smali/')
code = 'invoke-super {p0, p1}, Lcom/example/rat/activity/MainActivity;->onCreate(Landroid/os/Bundle;)V'
into = 'invoke-static,onCreate'
print(path+' '+code+' '+into)
The Application launches this Exception:
Traceback (most recent call last):
File "./bane.py", line 48, in <module>
login(creds[0],creds[1])
File "./bane.py", line 16, in login
bane = baneshell(username)
File "/mnt/c/Users/stecco/Desktop/bane/baneshell.py", line 16, in __init__
self.start()
File "/mnt/c/Users/stecco/Desktop/bane/baneshell.py", line 36, in start
self.execute(command)
File "/mnt/c/Users/stecco/Desktop/bane/baneshell.py", line 26, in execute
rat.rat(self.username)
File "/mnt/c/Users/stecco/Desktop/bane/modules/rat/rat.py", line 13, in __init__
self.start()
File "/mnt/c/Users/stecco/Desktop/bane/modules/rat/rat.py", line 18, in start
self.execute(command)
File "/mnt/c/Users/stecco/Desktop/bane/modules/rat/rat.py", line 42, in execute
inject.inject(command.replace('inject',''),self.address,self.port)
File "/mnt/c/Users/stecco/Desktop/bane/modules/rat/rat_modules/injector.py", line 101, in inject
injectcode(address,port)
File "/mnt/c/Users/stecco/Desktop/bane/modules/rat/rat_modules/injector.py", line 75, in injectcode
print(path+' '+code+' '+into)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Update: the code is printing nothing in method search before returning the fullname variable

OpenPYXL Python errors on cell values

I have an excel file with a ton of rows, and a column containing HZ, NZ or SZ now is this erroring out, and I have no idea on how to fix it.
region = ws.cell(row=srow, column=3)
if region.value == "HZ":
nregion = "Houston"
elif region.value == "NZ":
nregion = "North"
elif region.value == "SZ":
nregion = "South"
It errors with the following messages:
Traceback (most recent call last):
File "PowerExp.py", line 18, in <module>
wb=load_workbook(filename = wbfile)
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/excel.py", line 136, in load_workbook
_load_workbook(wb, archive, filename, use_iterators, keep_vba)
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/excel.py", line 171, in _load_workbook
style_table = read_style_table(archive.read(ARC_STYLE))
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/style.py", line 42, in read_style_table
font_list = parse_fonts(root, xmlns, color_index)
File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/style.py", line 160, in parse_fonts
font.size = font_node.find(QName(xmlns, 'sz').text).get('val')
AttributeError: 'NoneType' object has no attribute 'get'
I can't change the value and save the workbook as it is automatically retrieved from the email server and loaded into MySQL.

Categories

Resources