I have multiple sheets in excel converted from dataframe. I have collected the sheetnames in a list. I want to change the sheetname to the duplicate column values in which I have collected as shown below.
Here is my code:
dups = df.set_index('Group').index.get_duplicates()
After converting from dataframe to excel I have collected the sheetnames in a list.
xls = pd.ExcelFile('filename', on_demand=True)
sheets=xls.sheet_names
I also used as shown below:
for i in group: #names to be renamed, collected as list
wb=openpyxl.load_workbook('file.xlsx')
worksheet = wb.get_sheet_names()
worksheet.title = i
wb1.save('file.xlsx')
But, I got the AttributeError: 'list' object has no attribute 'title'.
Now, I want to rename the sheets to the dups value.
I would like to know if it is possible.
Pleased to hear some suggestions.
You can use openpyxl for this:
import openpyxl
file_loc = 'myexcel.xlsx'
workbook = openpyxl.load_workbook(file_loc)
worksheet = workbook.get_sheet_by_name('Sheet1')
worksheet.title = 'MySheetName'
workbook.save(file_loc)
You can run this in a loop to rename all the sheets. Let me know if this helps.
It is possible to iterate over the workbook using for sheet in wb
Here is an example:
import openpyxl
import os
os.chdir('C:\\Users\\Vahan\\Desktop\\xlsx')
wb = openpyxl.load_workbook('example.xlsx')
for sheet in wb: # or wb.worksheets
sheet.title = 'RenamedSheets'
wb.save('example.xlsx')
This functionality may help you achieve what you are trying to do.
Related
After saving my dataframe to a csv in a specific location, the csv file doesn't appear in the location I saved it to. Is there any reason why it possibly is not showing?
Here is the code to save my dataframe to csv:
df.to_csv(r'C:\Users\gibso\OneDrive\Documents\JOSEPH\export_dataframe.csv', index = False)
Even changing an empty df does not seem to work.
import pandas as pd
olympics={}
df = pd.DataFrame(olympics)
df.to_csv(r'C:\Users\gibso\OneDrive\Documents\JOSEPH\export_dataframe.csv', index = False)
Thanks for the help!
I would rather use the module openpyxl. Example of saving:
import openpyxl
workbook = openpyxl.Workbook()
sheet = workbook.active
# Work on your workbook. Once finished:
workbook.save(file_name) # file_name is a variable you must define
Don't forget installing openpyxl with pip first!
I am creating a class, where one of the methods "load()" wants to receive an excel file, retrieve the list of spreadsheets in the excel file and then return back the individual sheets of the file as a dictionary:
For example:
{“Sheet Name 1”: DataFrame,
“Sheet Name 2”: DataFrame,
“Sheet Name N”: DataFrame}
I am unsure of how best to do this. Other forums have suggested me to use xlrd or openpyxl but I have tried and can't solve this currently.
You can use openpyxl as follows
from openpyxl import load_workbook
workbook = load_workbook(filename="testing.xlsx")
# create the dictionary to hold the dataframes
data_dict = {}
# loop through each sheet, storing the sheetname and the dataframe
for sheet in workbook.sheetnames:
data_dict[sheet] = pd.read_excel('testing.xlsx', sheet_name=sheet)
I am using xlwings to place stock data I pull from the internet into worksheets. The workbook opens with a Sheet1, and upon running my program creates various sheets named according to the stock index. This leaves Sheet1 and causes problems with other methods I want to call. I want to test for any sheets that contain Sheet (plus an integer) and subsequently delete it similar to how you would test for the presence of a list element using the in operator. How would I go about doing this in xlwings? Current xlwings methods only allow sheets in which you manually name to be deleted.
My attempts have been rather lackluster. I've been trying loops to recognize the sheet names but to no avail. Here is my attempt to do so.
import xlwings as xw
wb = xw.Book('practice.xlsx')
for sheet in wb.sheets:
if 'Sheet' in sheet:
xw.Sheet[sheet].delete()
This works:
import xlwings as xw
wb = xw.Book('practice.xlsx')
for sheet in wb.sheets:
if 'Sheet' in sheet.name:
sheet.delete()
is the syntax more like sheet.api.Delete()? My xlwings is broken right now to check the exact syntax.
You can check, if the given sheet exist and you can delete the existing sheet and add a new one.
import xlwings as xw
def df_to_excel_util(excel,sheet_to_dataFrame_map):
with xw.App(visible=False) as app:
wb = app.books.open(excel)
current_sheets = [sheet.name for sheet in wb.sheets]
for sheet_name in sheet_to_dataFrame_map.keys():
if sheet_name in current_sheets:
wb.sheets[sheet_name].delete()
new_sheet = wb.sheets.add(after=wb.sheets.count)
new_sheet.range('A1').value = sheet_to_dataFrame_map.get(sheet_name)
new_sheet.name = sheet_name
wb.save()
if you have sheet object, you can delete as given below
sheet.delete()
if you want to delete a sheet with a given name.
wb.sheets['sheet_name'].delete
I'm trying to export a specific sheet from an excel file, but without a result. I want to export a specific sheet of paper to a completely new file What I have written is:
import openpyxl
book = openpyxl.load_workbook('C:\Python\test.xlsx')
a = (book.get_sheet_names())
sheet1 = book[a[5]]
sheet1.save('C:\Python\sheet2.xlsx')
Also, another thing I can't do,and look for a certain sheet if I have its name.
I apologize if the questions are simple, but it's been a few days since I started with python :)
Well, openpyxl does provide copy_worksheet() but it cannot be used between different workbooks. You can copy your sheet cell-by-cell or you can modify your starting workbook in memory and then you can save it with a different file name. Here is the code
import openpyxl
# your starting wb with 2 Sheets: Sheet1 and Sheet2
wb = openpyxl.load_workbook('test.xlsx')
sheets = wb.sheetnames # ['Sheet1', 'Sheet2']
for s in sheets:
if s != 'Sheet2':
sheet_name = wb.get_sheet_by_name(s)
wb.remove_sheet(sheet_name)
# your final wb with just Sheet1
wb.save('test_with_just_sheet2.xlsx')
I would like to access worksheets of a spreadsheet. I've copied the main workbook to another workbook using xlutils.copy(). But don't know the right way to access worksheets using xlwt module.
My sample code:
import xlrd
import xlwt
from xlutils.copy import copy
wb1 = xlrd.open_workbook('workbook1.xls', formatting_info=True)
wb2 = copy(master_wb)
worksheet_name = 'XYZ' (worksheet_name is a iterative parameter)
worksheet = wb2.get_sheet(worksheet_name)
Could someone please tell me what's the right command line to access the existing worksheets in a workbook using xlwt module? I know we can use 'add_sheet' method to add a worksheet in the existing workbook using xlwt module.
Any help, appreciated.
You can do sheets = wb1.sheets() to get a list of sheet objects, then call .name on each to get their names. To find the index of your sheet, use
[s.name for s in sheets].index(sheetname)
The sheets() method is curiously absent from the xlwt.Workbook class, so the other answer using that method will not work - only xlrd.book (for reading XLS files) has a sheets() method. Because all the class attributes are private, you have to do something like this:
def get_sheet_by_name(book, name):
"""Get a sheet by name from xlwt.Workbook, a strangely missing method.
Returns None if no sheet with the given name is present.
"""
# Note, we have to use exceptions for flow control because the
# xlwt API is broken and gives us no other choice.
try:
for idx in itertools.count():
sheet = book.get_sheet(idx)
if sheet.name == name:
return sheet
except IndexError:
return None
If you don't need it to return None for a non-existent sheet then just remove the try/except block. If you want to access multiple sheets by name repeatedly it would be more efficient to put them in a dictionary, like this:
sheets = {}
try:
for idx in itertools.count():
sheet = book.get_sheet(idx)
sheets[sheet.name] = sheet
except IndexError:
pass
Well, here is my answer. Let me take it step-by-step.
Considerting previous answers, xlrd is the right module to get the worksheets.
xlrd.Book object is returned by open_workbook.
rb = open_workbook('sampleXLS.xls',formatting_info=True)
nsheets is an attribute integer which returns the total number of sheets in the workbook.
numberOfSheets=rb.nsheets
Since you have copied this to a new workbook wb -> basically to write things, wb to modify excel
wb = copy(rb)
there are two ways to get the sheet information,
a. if you just want to read the sheets, use sheet=rb.sheet_by_index(sheetNumber)
b. if you want to edit the sheet, use ws = wb.get_sheet(sheetNumber) (this is required in this context to the asked question)
you know how many number of sheets in excel workbook now and how to get them individually,
putting all of them together,
Sample Code:
reference: http://www.simplistix.co.uk/presentations/python-excel.pdf
from xlrd import open_workbook
from xlutils.copy import copy
from xlwt import Workbook
rb = open_workbook('sampleXLS.xls',formatting_info=True)
numberOfSheets=rb.nsheets
wb = copy(rb)
for each in range(sheetsCount):
sheet=rb.sheet_by_index(each)
ws = wb.get_sheet(each)
## both prints will give you the same thing
print sheet.name
print ws.name