Insert image in existing excel sheet by using xlsxwriter - python

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')

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.

Python - Hide a worksheet

I am following the code to hide a worksheet using xlsxwriter. Anyone knows why this is failing?
https://xlsxwriter.readthedocs.io/example_hide_sheet.html
import xlsxwriter
import pandas as pd
file = 'C:/Prueba/GOOG.xlsx'
workbook = xlsxwriter.Workbook(file)
worksheet = workbook.get_worksheet_by_name('Sheet1')
worksheet.hide()
workbook.close()
I am getting the following error:
AttributeError: 'NoneType' object has no attribute 'hide'
The Excel has two worksheets: "Sheet1" and "Sheet2"
You have not set the column of your worksheet by worksheet.set_column()
Here is the correct code
import xlsxwriter
import pandas as pd
file = 'C:/Prueba/GOOG.xlsx'
workbook = xlsxwriter.Workbook(file)
worksheet = workbook.get_worksheet_by_name('Sheet1')
worksheet.set_column('A:A',30)
worksheet.hide()
workbook.close()

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.

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