Saving Workbook in Python using xlwt gives error - python

This problem has been happening to me a lot lately.
When I run this code:
import xlwt
wb = xlwt.Workbook()
sheet = wb.add_sheet("Random")
sheet.write(0,0,"hello!")
wb.save("test.xls")
It gives me the error:
Traceback (most recent call last):
File "<module2>", line 16, in <module>
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 662, in save
doc.save(filename, self.get_biff_data())
File "C:\Python27\lib\site-packages\xlwt\CompoundDoc.py", line 261, in save
f = open(file_name_or_filelike_obj, 'w+b')
IOError: [Errno 13] Permission denied: 'test.xls'
I've searched for the answer, but I couldn't find it.
Any help would be greatly appreciated!

Related

Not getting relative path in pycharm

i am unable to get the Relative path in pycharm
tried all the ways but was unable to succeed
This Test1.py is present in Pages/ folder
iam trying the code
import os
import xlrd
index = 0
#wb = xlrd.open_workbook(f"./data/search_flights_input_data.xlsx")
wb = xlrd.open_workbook(f".//data//search_flights_input_data.xlsx")
sheet = wb.sheet_by_index(index)
but getting the error as
C:\Users\User\AppData\Local\Programs\Python\Python38-32\python.exe C:/Users/User/Downloads/Selenium-Python-Example-main/Selenium-Python-Example-main/pages/test1.py
Traceback (most recent call last):
File "C:/Users/User/Downloads/Selenium-Python-Example-main/Selenium-Python-Example-main/pages/test1.py", line 9, in <module>
wb = xlrd.open_workbook(f".//data//search_flights_input_data.xlsx")
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\xlrd\__init__.py", line 111, in open_workbook
with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: './/data//search_flights_input_data.xlsx'
Process finished with exit code 1

Cannot convert the pdf file into the CSV file

I am a new python learner, I am struggling how to change the pdf file into CSV file by using Spyder.
Input
import tabula
dfs = tabula.read_pdf(r'C:\Users\home\Desktop\RN(G)_GazetteList.pdf.pdf', pages='all')
tabula.convert_into(r'\C:\Users\home\Desktop\RN(G)_GazetteList.pdf.pdf', "output.csv", output_format="csv", pages='all')
tabula.convert_into_by_batch("input_directory", output_format='csv', pages='all')
Output
The output file is empty.
Traceback (most recent call last):
File "C:\Users\home\.spyder-py3\temp.py", line 8, in <module>
tabula.convert_into(r'\C:\Users\home\Desktop\RN(G)_GazetteList.pdf.pdf', "output.csv", output_format="csv", pages='all')
File "C:\Users\home\anaconda3\lib\site-packages\tabula\wrapper.py", line 273, in convert_into
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)
FileNotFoundError: [Errno 2] No such file or directory: '\\C:\\Users\\home\\Desktop\\RN(G)_GazetteList.pdf.pdf'
Thank you so much
As a possible answer to serve as a reference. You could use pdftables_api:
import pdftables_api
conversion = pdftables_api.Client('key')
conversion.csv('pdf_path','output_path')

How to get permission for file editing in python(pandas)

I am trying to modify existing excel file on Windows using Python via Pandas, but the program gives me an error.
This is a sample of my simple program:
df_read = pd.read_excel("C:\\Users\\77888\\Desktop\\HKR_ОТЧЕТЫ\\Ноябрь 2020\\2020-11-03.xlsx")
df = pd.DataFrame({"Время":[1], "Сумма счета":[1], "Столы":[1], "Заказ":[1]})
df = df.append({"Время":1, "Сумма счета":1, "Столы":1, "Заказ":1}, ignore_index = True)
path = "C:\\Users\\77888\\Desktop\\HKR_ОТЧЕТЫ\\Ноябрь 2020\\2020-11-03.xlsx"
assert os.path.isfile(path)
writer = pd.ExcelWriter(path, engine = 'xlsxwriter')
writer.save()
Here is the error:
Traceback (most recent call last):
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\site-packages\xlsxwriter\workbook.py", line 320, in close
self._store_workbook()
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\site-packages\xlsxwriter\workbook.py", line 638, in _store_workbook
raise e
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\site-packages\xlsxwriter\workbook.py", line 635, in _store_workbook
xlsx_file = ZipFile(self.filename, "w", compression=ZIP_DEFLATED,
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1239, in init
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'C:\Users\77888\Desktop\HKR_ОТЧЕТЫ\Ноябрь 2020\2020-11-03.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\77888\Desktop\HKR_ОТЧЕТЫ\check.pu", line 12, in
writer.save()
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\excel_xlsxwriter.py", line 193, in save
return self.book.close()
File "C:\Users\77888\AppData\Local\Programs\Python\Python39\lib\site-packages\xlsxwriter\workbook.py", line 322, in close
raise FileCreateError(e)
xlsxwriter.exceptions.FileCreateError: [Errno 13] Permission denied: 'C:\Users\77888\Desktop\HKR_ОТЧЕТЫ\Ноябрь 2020\2020-11-03.xlsx'
[Finished in 1.531s]
The most common cause of this issue on Windows is that the xlsx file being created is already open in Excel. For example:
# A simple pandas/xlsxwriter program.
C:\jmcnamara>type pandas_simple.py
import pandas as pd
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
# Run the program.
C:\jmcnamara>python pandas_simple.py
# Open the file in Excel.
C:\jmcnamara>pandas_simple.xlsx
# Try to rerun the program while the file is open.
C:\jmcnamara>python pandas_simple.py
Traceback (most recent call last):
File "C:\python\xlsxwriter\workbook.py", line 320, in close
File "C:\python\xlsxwriter\workbook.py", line 638, in _store_workbook
File "C:\python\xlsxwriter\workbook.py", line 636, in _store_workbook
File "C:\python\zipfile.py", line 1204, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'pandas_simple.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pandas_simple.py", line 8, in <module>
writer.save()
File "C:\python\site-packages\pandas\io\excel.py", line 1732, in save
return self.book.close()
File "C:\python\xlsxwriter\workbook.py", line 322, in close
xlsxwriter.exceptions.FileCreateError: [Errno 13] Permission denied: 'pandas_simple.xlsx'
As you can see the error is similar to yours.
This warning can also happen if you don't have write permissions for the output directory. That should be easy to check by trying to create a file in the target directory.
right click and run code with administrator permissions.
Through changing the privileges by running the programs you want use as "Run as administrator" for example I was using Excel in Pycharm so I changed to administrator privileges for both excel and Pycharm and got no error.

OSError: [Errno 22] Invalid argument in load_workbook

When I use openpyxl operating excel, the code below raises the error. I cannot figure out the the reason.
wb=load_workbook(r'C:\Users\Administrator\Desktop\11.xls')
Error message:
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe
C:/Users/Administrator/Desktop/data_process.py
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/data_process.py", line 3, in <module>
wb = load_workbook(r'‪C:\Users\Administrator\Desktop\11.xls')
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\reader\excel.py", line 171, in load_workbook
archive = _validate_archive(filename)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\reader\excel.py", line 118, in _validate_archive
archive = ZipFile(filename, 'r', ZIP_DEFLATED)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\zipfile.py", line 1082, in __init__
self.fp = io.open(file, filemode)
OSError: [Errno 22] Invalid argument: '\u202aC:\\Users\\Administrator\\Desktop\\11.xls'
Try the following,
instead of 1 '\' try using double \
from openpyxl import load_workbook
wb=load_workbook(r'C:\\Users\\Administrator\\Desktop\\11.xls')
Let me know if this works.

Unable to open xlsx file with xlrd

I am getting an error file is not support in xlrd-0.7.1.
The file is saved in xlsx format
Traceback (most recent call last):
File "C:\Users\jawed\workspace\test\Excelproject.py", line 8, in <module>
workbook=xlrd.open_workbook(file_location)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 425, in open_workbook
on_demand=on_demand,
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 878, in biff2_8_load
f = open(filename, open_mode)
IOError: [Errno 2] No such file or directory: 'C:\\Users\\jawed\\workspace\\IAMarks.xls'
The file doesn't exist.
Check the location of the file before calling the function:
import os
if os.path.isfile(file_location):
workbook = xlrd.open_workbook(file_location)
else:
# tell the user they've done something wrong
A possibly more Pythonic way to do it (see EAFP) is in a try/except block:
try:
workbook = xlrd.open_workbook(file_location)
except IOError as error:
print(error)
# tell the user they've done something wrong

Categories

Resources