Inserting object in excel using Python - python

I want to insert a Image(object) in MS-Excel Report which i am generating using openpyxl utility. Is there a way to do it using some python utility?

Openpyxl allows you to write images into your Excel files! Here it is in the official documentation.
import openpyxl
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
picture = openpyxl.drawing.Image('/path/to/picture')
picture.anchor(ws.cell('cell to put the image'))
ws.add_image(picture)
wb.save('whatever you want to save the workbook as')
This code of course refers to creating a new workbook and adding the image into it. To add the image to your preexisting workbook you would obviously just load that workbook using load_workbook.

Related

openpyxl save workbook to file with path

I use Openpyxl in Python to write some data in an Excel file using Workbook.
When I want to save the workbook to a file, I can only provide a filename argument, so I can not write files in other directories.
here is my sample code:
import openpyxl
file_name = "sample.xlsx"
wb = openpyxl.Workbook()
# writing some data to workbook
wb.save(filename=file_name)
I have checked the documentation via this link and found nothing more.
Python: 3.10.7
Openpyxl: 3.0.10
Can you help me provide workarounds to solve this problem?
You can indicate a full path + filename, and it will create a file where you need it:
wb = Workbook()
wb.save(r'D:\folder\folder\folder\Filename.xlsx')

Is there a way of editing xlsx workbooks containing images in Python?

Is there a way to edit and save xlsx-workbooks that contain images, charts, figures etc. through Python?
A few things I've tried:
openpyxl:
openpyxl does currently not read all possible items in an Excel file so images and charts will be lost from existing files if they are opened and saved with the same name.
xlwt:
xlwt is a library for writing data and formatting information to older Excel files (ie: .xls)
xlutils:
Utilities for working with Excel files that require both xlrd and xlwt
Related.
The XLWings module seems to be the only module that supports this:
http://docs.xlwings.org/en/stable/quickstart.html. Unlike other Python / Excel modules it seems to use COM automation under the hood to connect to Excel.
import xlwings as xw
wb = xw.Book() # this will create a new workbook
wb = xw.Book('FileName.xlsx') # connect to an existing file in the current working directory
wb = xw.Book(r'C:\path\to\file.xlsx') # on Windows: use raw strings to escape backslashes
app = xw.App() # or something like xw.apps[0] for existing apps
wb = app.books['Book1']

Creating workbook and worksheet using openpyxl

I am trying to load an existing Excel file and create a new sheet inside that workbook, but my code is not working using openpyxl.
rb = load_workbook("C:\Raw_Dump.xlsx")
rb.create_sheet("Sheet2")
sheet1 = rb.worksheets[0]
Any help would be appreciated.
You have to save the workbook to the same filename:
rb.save(r"C:\Raw_Dump.xlsx")
full working example:
import openpyxl
ws_name = r"Raw_Dump.xlsx"
rb = openpyxl.load_workbook(ws_name)
rb.create_sheet("Sheet2")
rb.save(ws_name)
I spent a long time searching this and found the best way is to do sheet removal. The code below worked for me:
for sheet in wb.sheetnames:
if sheet not in "MY_SHEET_I_WANNA_KEEP":
rm_sheet = wb[sheet];
wb.remove_sheet(rm_sheet)
wb.save("JustOneSheet.xlsx")

Python append xls file using only xlwt/xlrd

I am having problems appending issues appending data to an xls file.
Long story short, I am using a program to get some data from something and writing it in an xls file.
If I run the script 10 times, I would like the results to be appended to the same xls file.
My problem is that I am forced to use Python 3.4 and xlutils is not supported, so I cannot use the copy function.
I just have to use xlwt / xlrd. Note, the file cannot be a xlsx.
Is there any way i can do this?
I would look into using openpyxl, which is supported by Python 3.4. An example of appending to a file can be found https://openpyxl.readthedocs.org/en/default/. Please also see: How to append to an existing excel sheet with XLWT in Python. Here is an example that will do it. Assuming you have an Excel sheet called sample.xlsx:
from openpyxl import Workbook, load_workbook
# grab the active worksheet
wb = load_workbook("sample.xlsx")
ws = wb.active
ws.append([3])
# Save the file
wb.save("sample.xlsx")

copy a worksheet in openpyxl

I'm using openpyxl (unfortunately I don't know how to find out my version number, installed it about a month ago) on Windows with python 2.7 and want to copy a worksheet that I generated using a template.xlsx file to a new workbook. The template has a single worksheet that I alter. I want to load it n times and copy each version as a new worksheet to another workbook. Could also be the same workbook ifneedbe.
I found some hints here which took me here. The example doesn't work as it seems the add_sheet() method has been removed.
primary.add_sheet(copy.deepcopy(ws),ido+1)
AttributeError: 'Workbook' object has no attribute 'add_sheet'
Also couldn't find anything helpful in the API.
I'm afraid copying worksheets is not supported because it is far from easy to do.
I was struggling with it as you. But, I could find out the way to solve.
The best way I think to copy Excel worksheets using Openpyxl and Python:
from openpyxl import Workbook, load_workbook
# workbook source = wb1 and workbook destination = wb2
wb1 = load_workbook('file.xlsx')
wb2 = Workbook()
ws1 = wbs.active
ws2 = wbd.active
for r in plan1.iter_rows():
for c in r:
ws2[c.coordinate] = c.value
wb2.save('file2.xlsx')
The FOR loop with iter_rows() creates a named list with existing filled cells. And the 2nd FOR iterates in those cells ('A1','A2','B1' etc). The method .coordinate can be applied to the cell(c) and extract the Column,Row like 'A1' as a string. If we add it as an index of the worksheet, we can set it as a variable. Then just get the value of the cell(c), the magic is done.
We can do something with data during the loop and after save it to the file.

Categories