saving an existing worksheet as html in python - 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)

Related

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.

Unable to read excel file , list index out of range error, cant find Sheets

I am trying to read excel (.xlsx) file and convert it to dataframe. I used pandas.ExelFile , pandas.read_excel, openpyxl load_workbook and even io file reading methods but i am unable to read Sheet of this file. Every time i get list index out of range error or no sheet names is case of openpyxl. Also tried xlrd method.
temp_df = pd.read_excel("v2s.xlsx", sheet_name = 0)
or
temp_df = pd.read_excel("v2s.xlsx", sheet_name = "Sheet1")
or
from openpyxl import load_workbook
workbook = load_workbook(filename="v2s.xlsx",read_only = True, data_only = True)
workbook.sheetnames
Link to excel file
According to this ticket, the file is saved in a "slightly defective" format.
The user posted that he used Save As to change the type of document back to a normal Excel spreadsheet file.
Your file is this type:
You need to save it as:
Then running your code
from openpyxl import load_workbook
workbook = load_workbook(filename="v2s_0.xlsx",read_only = True, data_only = True)
print(workbook.sheetnames)
Outputs:
['Sheet1']

There's an "AttributeError: 'Sheet' object has no attribute 'write'"

I'm trying to write a value into an excel document and it gives me an error.
This is on Pycharm and I want it to write a value if there is nothing there
import xlrd
import xlwt
import xlsxwriter
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
if sheet.cell(4,4).value == xlrd.empty_cell.value:
sheet.write(4,4, '120')
I'd expect it to just write on the excel document but it isn't working.

Insert image in existing excel sheet by using xlsxwriter

i am trying to add image into the existing excel sheet by using xlsxwriter module
import xlsxwriter
workbook = xlsxwriter.Workbook('C:/Users/Desktop/blank.xlsx')
worksheet = workbook.get_worksheet_by_name('Sheet1')
worksheet.insert_image('B5', 'C:/Users/Desktop/CaseDeatails/Abc.jpg')
i am getting the below error
Traceback (most recent call last):
File "C:\Users\Desktop\insertImage.py", line 23, in
worksheet.insert_image('B5', 'C:/Users/Desktop/CaseDeatails/Abc.jpg')
AttributeError: 'NoneType' object has no attribute 'insert_image'
Please help me on this error
inseart image in xlsxwriter
import xlsxwriter
import os
workbook = xlsxwriter.Workbook('C:/Users/Desktop/blank.xlsx')
worksheet = workbook.get_worksheet_by_name('Sheet1')
image = os.path.join(settings.BASE_DIR, "C:/Users/Desktop/CaseDeatails/", "Abc.jpg")
worksheet.insert_image('B5', image)
That isn't possible with XlsxWriter since it cannot read or modify an existing file.
Try the OpenPyXL module instead.
Also for some reason python doesn't like the links to be:
C:/Users/Desktop/blank.xlsx
They have to have a double /, so it should be:
C://Users//Desktop//blank.xlsx
It is not a xlsxwriter solution, but it works well:
from openpyxl import Workbook
from openpyxl.drawing.image import Image
wb = Workbook()
sheet1 = wb.create_sheet('sheet1',0)
active = wb['sheet1']
active.add_image(Image('fig.png'),'A1')
wb.save('myfile.xlsx')

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)

Categories