I have a code to work with an already open Excel file (it actually just closes it so the rest of the code can work, and then opens it again at the end). It works in one of my computers, but not in the other - I'm getting the following error:
Traceback (most recent call last):
File "tkinter\__init__.py", line 1892, in __call__
file "executavel.py", line 48, in GetSquareRoot
File "site-packages\win32com\client\dynamic.py", line 527, in __getattr__
AttributeError: Excel.Application.Workbooks
Here is the part of the code that raises the error (line 48 is "wb = xlApp.Workbooks.Open(path)"):
xlApp = client.GetObject(None, "Excel.Application")
wb = xlApp.Workbooks.Open('planilha.xlsx')
wb.Save()
wb.Close()
All Excel windows are closed except for the spreadsheet in question (there are no open pop-ups or anything). Is there a way to make this code work in both computers, or another way to close an already open Excel workbook?
Thanks!
Related
I am trying to download a file from my MEGA account using the following code from the mega.py library of Python:
from mega import Mega
mega = Mega()
m = mega.login('example#example.com', 'example')
file = m.find('example.txt')
m.download(file, 'D:\\Desktop')
However, it is always returning:
Traceback (most recent call last):
File "D:\Programas\aNaconda\lib\shutil.py", line 788, in move
os.rename(src, real_dst)
PermissionError: [WinError 32] The file is already being used by another process: 'C:\\Users\\vrida\\AppData\\Local\\Temp\\megapy_xdste432' -> 'example.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<ipython-input-26-c3f75106fafb>", line 1, in <module>
m.download(file)
File "D:\Programas\aNaconda\lib\site-packages\mega\mega.py", line 564, in download
return self._download_file(file_handle=None,
File "D:\Programas\aNaconda\lib\site-packages\mega\mega.py", line 745, in _download_file
shutil.move(temp_output_file.name, output_path)
File "D:\Programas\aNaconda\lib\shutil.py", line 803, in move
os.unlink(src)
PermissionError: [WinError 32] The file is already being used by another process: 'C:\\Users\\vrida\\AppData\\Local\\Temp\\megapy_example'
Actually when I enter in the folder (C:\Users\vrida\AppData\Local\Temp) I find a temporary file like the one I'm wanting to download but named megapy_example.
I saw that the following site has a discussion to solve the problem:
https://www.reddit.com/r/learnpython/comments/mw6is2/download_file_from_mega_using_megapy/
asking to add the following lines to the code:
try:
m.download(file, 'D:\\Desktop')
except PermissionError:
continue
In my case, the continue command wasn't working so I simply put in the pass command. The code runs, but I don't know if the file is really saved or not.
Could someone please help me? I really need to download the files and save them.
If it doesn't work through the mega.py library, you guys would somehow know how to download from the public link like this by Python:
https://mega.co.nz/#!cSZCELDb!5O57KMVMIgrPiH5fnaefWeNPDqoDWzGbY-sZkdTUdNk
There's a bug in the library, it's not closing the file before moving it. You could fix the bug by editing the source code:
Open the file at D:\Programas\aNaconda\lib\site-packages\mega\mega.py
Goto line 745 where the line shutil.move(temp_output_file.name, output_path) is.
Add temp_output_file.close() right above it.
Save & try again.
I have been learning Python PyPDF2, This was the code on geeksforgeeks.org/
# importing required modules
import PyPDF2
# creating a pdf file object
pdfFileObj = open('English.pdf', 'rb')
# creating a pdf reader object
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
# printing number of pages in pdf file
print(pdfReader.numPages)
# creating a page object
pageObj = pdfReader.getPage(0)
# extracting text from page
print(pageObj.extractText())
# closing the pdf file object
pdfFileObj.close()
After running this program, this error pops up:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python38-32\lib\site-packages\PyPDF2\pdf.py", line 1147, in getNumPages
self.decrypt('')
File "C:\Program Files (x86)\Python38-32\lib\site-packages\PyPDF2\pdf.py", line 1987, in decrypt
return self._decrypt(password)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\PyPDF2\pdf.py", line 1996, in _decrypt
raise NotImplementedError("only algorithm code 1 and 2 are supported")
NotImplementedError: only algorithm code 1 and 2 are supported
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 11, in <module>
print(pdfReader.getNumPages())
File "C:\Program Files (x86)\Python38-32\lib\site-packages\PyPDF2\pdf.py", line 1150, in getNumPages
raise utils.PdfReadError("File has not been decrypted")
PyPDF2.utils.PdfReadError: File has not been decrypted
I tried different ways to resolve this, but this error stays, can you please guide me here?
PyPDF2 only supports very old PDF files. It doesn't support the formats from Acrobat 6. You'll either need to convert this to an older format or find a different PDF library.
https://github.com/mstamy2/PyPDF2/issues/378
I'm trying to use Ezsheets through IDLE and I managed to set it up ok, but when I go to open a Google sheet I keep having this error come up. Have I put the file in the wrong place? I tried re-downloading the credential-sheets.json to no avail.
>>> import ezsheets
>>> ss = ezsheets.Spreadsheet('1yX5ysHDlc086WnjNWX_4RVAYKqefW9YrBTVWzGIQS2Q')
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
ss = ezsheets.Spreadsheet('1yX5ysHDlc086WnjNWX_4RVAYKqefW9YrBTVWzGIQS2Q')
File "C:\Users\User\AppData\Roaming\Python\Python39\site-packages\ezsheets\__init__.py", line 183, in __init__
init() # Initialize this module if not done so already.
File "C:\Users\User\AppData\Roaming\Python\Python39\site-packages\ezsheets\__init__.py", line 1669, in init
raise EZSheetsException(
ezsheets.EZSheetsException: Can't find credentials file at C:\Users\User\AppData\Local\Programs\Python\Python39-32\credentials-sheets.json. You can download this file from https://developers.google.com/sheets/api/quickstart/python and clicking "Enable the Google Sheets API". Rename the downloaded file to credentials-sheets.json.
I am writing to a file from python, if the file is open in some process, python throws error.
To make it clear, I am writing to a excel file, I want it closed if already open.
This is below code I use to write to file -
writer = pd.ExcelWriter('file_Output.xlsx', engine='xlsxwriter')
file.to_excel(writer,index=False, sheet_name='Sheet1')
Which throws below error if the file - file_Output.xlsx is already open in excel.
Traceback (most recent call last):
File "pythonclose.py", line 311, in <module>
writer.save()
File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\excel.py", line 1952, in save
return self.book.close()
File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\site-packages\xlsxwriter\workbook.py", line 306, in close
self._store_workbook()
File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\site-packages\xlsxwriter\workbook.py", line 655, in _store_workbook
allowZip64=self.allow_zip64)
File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\zipfile.py", line 1082, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'file_Output.xlsx'
If a file is open by an external program(such as MS Excel in this case), the file handle or control of the file is with the program. Python has no control over the file and cannot close/edit it. However, if you have the Process ID for the file, it is possible to kill the process by different means(for eg. using taskkill in Windows or even with Python).
This is a very very strange issue. I have quite a large excel file (the contents of which I cannot discuss as it is sensitive data) that is a .xlsx and IS a valid excel file.
When I download it from my email and save it on my desktop and try to open the workbook using xlrd, xlrd throws an AssertionError and does not show me what went wrong.
When I open the file using my file browser, then save it (without making any changes), it works perfectly with xlrd.
Has anyone faced this issue before? I tried passing in various flags to the open_workbook function to no avail and I tried googling for the error. So far I haven't found anything.
The method I used was as follows
file = open('bigexcelfile.xlsx')
fileString = file.read()
wb = open_workbook(file_contents=filestring)
Please help! The error is as follows
Traceback (most recent call last):
File "./varify/samples/resources.py", line 354, in post
workbook = xlrd.open_workbook(file_contents=fileString)
File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/__init__.py", line 416, in open_workbook
ragged_rows=ragged_rows,
File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/xlsx.py", line 791, in open_workbook_2007_xml
x12sheet.process_stream(zflo, heading)
File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/xlsx.py", line 528, in own_process_stream
self_do_row(elem)
File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/xlsx.py", line 722, in do_row
assert tvalue is not None
AssertionError
rename or Save as your Excel file as .xls instead of .xlsx
Thank You
Use pyopenxl, not xlrd, for this format: https://openpyxl.readthedocs.org/en/latest/