Creating workbook and worksheet using openpyxl - python

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

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

open excel file on sheet1 after changes with xlwings

I need an Excel workbook to open on sheet1 or by that sheets name. I'm using xlwings to alter a workbook with several sheets. Everything works fine but when the file is opened in Excel it opens on the last sheet that has been altered. I can't seem to find an answer. Please help. Thanks
This is what might help you
# load Workbook
workbook= xw.Book('path_file')
#acivate sheet by name
sheet = workbook.sheets.activate('Sheet_Name')
if that doesn't work try
sheet = workbook.sheets["Sheet_Name"]

Getting sheet names from a large Excel file

I am using Python 3.7 and using OpenPyxl to read the sheet names from a large excel workbook (29MB) with 10 tabs.
import openpyxl
from openpyxl import load_workbook
wb = load_workbook(filename='h:\\Master_Portfoliio.xlsx')
print(wb.sheetnames)
The code above works for smaller files but when I use the same code for this file, the code just hangs. I would like to read the sheet names and then remove a tab and then copy a tab from another excel workbook into this workbook.
Have you tried the read_only = True flag?
wb = load_workbook(filename='h:\\Master_Portfoliio.xlsx', read_only = True)

Inserting object in excel using 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.

How to iterate over worksheets in workbook, openpyxl

I've been using the openpyxl module to do some processing on some .xlsx files. I've been trying to figure out how to iterate over sheets in a workbook. I'm not sure if I can get it figured out. I've tried the 2 codes below which both return empty results. My .xlsx file has about 20 sheets, so something should return.
The one thing I couldn't find on the internet, is how to set a workbook to an actual workbook. Usually I am writing to a workbook, so I just initialize it by setting a variable to en empty workbook workbook = Workbook() but in this case, I am unsure if I can open a workbook by doing workbook = Workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
If anyone can identify what it is I am doing wrong, I would appreciate it.
Here is my code:
workbook = Workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
for sheet in workbook.worksheets:
print sheet
# or
for sheet in workbook.worksheets:
print sheet.title
Open the workbook via load_workbook() and iterate over worksheets:
from openpyxl import load_workbook
wb = load_workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
for sheet in wb.worksheets:
print(sheet)
Here's one if you need active worksheets for your code
for sheet in wb:
ws = wb[sheet]
print('Now in sheet: ' + ws.title)
To print titles of all sheets in a workbook:
from openpyxl import load_workbook
wb = load_workbook(r"C:\Excel\LOOKUP_TABLES_edited.xlsx")
print(wb.sheetnames)
I struggled a bit with the whole "workbook.active" and didn't know how to get around it so I tried a little bit of everything and here is what worked well for me!
for sheet in workbook.sheetnames[2:len(workbook.sheetnames)]:
ws = workbook[value]
for val in ws.iter_rows(min_row=11, max_row=21, min_col=2, max_col=10, values_only=True):
print(str(sheet) + " " + str(val))
This will print the sheet name starting with the third sheet, since that's what I needed, as well as all the cell values referenced. The only other thing is this prints a list and if you want to iterate through each value and pull out "0s" or "None" values, then you'll need another loop. Hope this helps whoever else is looking!

Categories