unable to save workbook in xlwings - python

Unable to save a workbook using xlwings. Below is the code, loc contains the excel file name
import xlwings as xw
from xlwings import Book
print(xw.__version__)
app = xw.App(visible=False)
wb = Book(loc)
wb.sheets['crosst'].range('AJ2:CH9999').value=''
wb.save(loc)
app.quit()
below is the response, but when I check the Excel file, the contents are not cleared?
0.24.6
Process finished with exit code 0

Save the file under a new name, not under the same name.
import xlwings as xw
from xlwings import Book
print(xw.__version__)
app = xw.App(visible=False)
wb = Book(loc)
wb.sheets['crosst'].range('AJ2:CH9999').value=''
wb.save(loc1)
app.quit()
where loc1 is another name.
Ensure also that your cells are correct.

Related

Using XLwings to update Excel File from Python : OSError: [WinError -2147467259] Unspecified error

I am using xlwings in python to read excel file and add my dataframe in excel file. Earlier my code was working fine but since yesterday its giving error as below :
OSError: [WinError -2147467259] Unspecified error
Code:
app = xw.App(visible=False)
wb = xw.Book(file_path)
ws = wb.sheets[1]
ws.range(A2).options(index=False).value = df
wb.save()
wb.close()
app.quit()
Error is coming on the second line of code where it tries to open the book.
wb = xw.Book(file_path)
Complementary to moken's comment: Using with xw.App(visible=False) as app: (available since Version 0.24.3) is more stable than app = xw.App(visible=False), because it ensures that everything is properly cleaned up again and to prevent zombie processes after an error. Here is an example:
import xlwings as xw
path = r"test.xlsx"
with xw.App(visible=False) as app:
wb = xw.Book(path)
ws = wb.sheets[1]
ws.range("A2").options(index=False).value = df
wb.save(path)
wb.close()

Openpyxl is not saving the excel file, why?

here is my code:
import openpyxl as op
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl import worksheet
path = ...
op.load_workbook(path)
wb = Workbook()
ws2 = wb.create_sheet('Sheet2')
wb.save(filename = 'NameOfTheFile.xlsx')
Now everything works, except saving the file, when I add:
print(wb.sheetnames)
it does print out the Sheet2 I just added, but for some reason when I go to the documents where the file is, it does not update it, that sheet is nowhere to be found.

Open and modify specific worksheet in excel workbook

I am trying to open and format a specific Excel worksheet. However I am having trouble trying to find out how to look at a specific worksheet.
The code I'm trying to use to open the workbook and go to a specific worksheet and then change the font for specific cells is:
from openpyxl import Workbook
def applyValidations(path,tabname):
workbook = Workbook(path)
worksheet = workbook[tabname]
c = worksheet['A1:A5']
c.font = Font(size=22)
The error I'm getting is:
KeyError: 'Worksheet Department Data does not exist.'
Department Data is the name of the worksheet which does exist in the workbook.
Here's some code I use regularly with openpyxl, which may solve your Q:
from openpyxl import load_workbook
wb = load_workbook(filename=data_file, read_only=True)
ws = wb.active
print(wb.sheetnames)

Is there a way to edit/save an opening (in excel) xlsx file?

I want to do something like this:
import openpyxl as x
wb = x.load_workbook(filename)
# do some edit to the workbook
wb.save(filename)
The file specified by the filename is opening in Excel. Excel is locking the file so I will get permission denied error running the above code. Is there a way to edit/save it?
from openpyxl import load_workbook
ifile = 'Whales.xlsx'
wb = load_workbook(filename=ifile)
# do some edit to the workbook
wb.save(ifile)

saving an existing worksheet as html in python

I went through these link and other links for xlwt, xlrd and xlutils
writing to existing workbook using xlwt
from xlutils.copy import copy
from xlrd import open_workbook
from xlwt import easyxf
rb = open_workbook('example.xls',formatting_info=True)
r_sheet = rb.sheet_by_index(0)
print r_sheet
print r_sheet.name
wb = copy(rb)
w_sheet = wb.get_sheet(0)
print w_sheet.name
w_sheet.save('example.html') #it throws an error in the last line saying "AttributeError: 'Worksheet' object has no attribute 'save'"
How can I save the worksheet alone as HTML file uing python
I am not sure you can save and excel sheet as html just with xlwt or xlrd.
One alternative is to use pandas which internally uses xlrd and xlwt saving you of coding all the steps involved.
You can read your excel sheet with
df = pandas.read_excel('example.xls', sheetname='sheet1')
and get it as html with:
html = df.to_html()
html is a string you can save in a file with open(myfile, 'w').write(html)

Categories