openpyxl creates spreadsheet with name "Sheet1" by default
even though I'm specifying that the sheet should be created with index 0.
I'm sure it's not a duplicate question.
Documentation says openpyxl supports 2010 MS Office version but I'm using office365 pro.
Could you provide any help or suggestions?
Please note that standalone code is working fine for me but when the same code is being integrated with other code I experience the problem described below.
I have tried many things. Since I'm new to Python it looks like there's something I'm not aware of.
If I specify index 1 there are two worksheets created: one with name Sheet and the other one with name I'm providing. If I provide Index 0 there is only one Sheet with name Sheet1 created.
The code below should create worksheet at index 0 with name test.
for r in range(3, rowcount + 1):
for c in range(1, columncount + 1):
final_path = first_part + str(r) + second_part + str(c) + third_part
table_data = self.driver.find_element_by_xpath(final_path).text
fname = r"{}_{}.xlsx".format(str(i[1]), str(i[2]))
if (os.path.exists(fname)):
workbook = openpyxl.load_workbook(fname)
worksheet = workbook[fname]
else:
workbook = Workbook()
worksheet= workbook.create_sheet(fname,0)
#worksheet = workbook.active
#worksheet.title = fname
worksheet.cell(row=r,column=c).value = table_data
workbook.save(fname)
openpyxl creates a single sheet called "Sheet" when you first call the Workbook() function. It's just a kind of annoying quirk of the module.. The cleanest way I found to deal with it is to just rename that sheet instead of attempting to make a new one
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.title = "My sheet name"
wb.save("Test.xlsx")
Will create an xlsx file with a single worksheet called "My sheet name".
When you call create_sheet with index 0, you just insert a new sheet before this original sheet.
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.title = "My sheet name"
ws2 = wb.create_sheet("Another Name", 0)
wb.save("Test.xlsx")
Will create an xlsx where the first sheet is called "Another name" and the second sheet is called "My sheet name". If you're dealing with one sheet workbooks it's easiest to just use title.
You can remove this sheet via:
from openpyxl import Workbook
workbook = Workbook()
del workbook['Sheet']
You can get that sheet and remove like this :
sheet = wb1.get_sheet_by_name('Sheet')
wb1.remove_sheet(sheet)
Related
I have one big excel,including several sheets. Now I need to save every sheet in one excel. Now, I finish and some cells which have formulas have value in the new excel. But I find one new problem,how can I save every sheets which keeps the original style (format) such as red background?I checked the former question, but still get no answer. Editing workbooks with rich text in openpyxl
from openpyxl import load_workbook,Workbook
wb = load_workbook("test11.xlsx",data_only=True)
sheetnames = wb.sheetnames
for name in sheetnames:
ws = wb.get_sheet_by_name(name)
print(ws)
wb2 = Workbook()
ws2 = wb2.active
for i,row in enumerate(ws.iter_rows()):
for j,cell in enumerate(row):
ws2.cell(row=i+1, column=j+1, value=cell.value)
ws2.title = name
wb2.save(name + ".xlsx")
Every cell in openpyxl has a .style attribute that you can call and set. Your code would be this:
from openpyxl import load_workbook,Workbook
wb = load_workbook("test11.xlsx",data_only=True)
sheetnames = wb.sheetnames
for name in sheetnames:
ws = wb.get_sheet_by_name(name)
print(ws)
wb2 = Workbook()
ws2 = wb2.active
for i,row in enumerate(ws.iter_rows()):
for j,cell in enumerate(row):
c = ws2.cell(row=i+1, column=j+1, value=cell.value)
c.style = cell.style
ws2.title = name
wb2.save(name + ".xlsx")
You could consider the following option instead.
Basically this code makes a copy of the original xlsx file and deletes the unwanted sheets before saving with the sheet name. Since it is a copy of the original it should retain all the styling etc of each sheet.
from openpyxl import load_workbook
sheetnames = load_workbook('test11.xlsx').sheetnames
for name in sheetnames:
wb = load_workbook("test11.xlsx")
print(wb[name])
for delsheet in sheetnames:
if delsheet != name:
del wb[delsheet]
wb.calculation.calcMode = 'auto' # set formula calculation to auto
wb.save(name + ".xlsx")
I have an optimization problem that runs in a for loop. I want the results of each new iteration to be saved in a different tab in the same workbook.
This is what I'm doing. Instead of giving me multiple tabs in the same workbook, I'm getting multiple workbooks.
from openpyxl import Workbook
wb1 = Workbook()
for i in range(n):
ws = wb1.active()
ws.title = str(i)
#code on formatting sheet, optimization problem
wb1.save('outfile'+str(i)+'.xlsx')
Every iteration you are grabbing the same worksheet - ws = wb1.active() - and then simply saving your results to a different workbook.
You simply need to create a new sheet on each iteration. Something like this:
from openpyxl import Workbook
wb1 = Workbook()
for i in range(n):
ws = wb1.create_sheet("run " + str(i))
#code on formatting sheet, optimization problem
wb1.save('outfile.xlsx')
Notice that the save is indented out to simply save the file once all worksheets have been formatted. It is not necessary to save on each iteration. The saving operation can take time, especially when adding more tabs.
This code will create Excel Workbook containing worksheets same as the number of strings in a text file taken as the input. Here i have a text file named 'sample.txt' having 3strings. This code will so create 3 worksheets in a workbook named 'reformatted.data.xls'.
Also i have removed the default worksheets that get created automatically when the workbook object is created.
import xlwt
from openpyxl import Workbook
wb1 = Workbook()
row = 0
f = open('C:\Desktop\Mytestcases\sample.txt')
lines = f.readlines()
for i in range(len(lines)):
ws = wb1.create_sheet("worksheet" + str(i))
ws.cell(row=1, column=1).value = lines[i]
row += 1
sheet = wb1.get_sheet_by_name('Sheet')
wb1.remove_sheet(sheet)
wb1.save('reformatted.data.xls')
row = 5
column = 0
writer = pd.ExcelWriter(file_name, engine='openpyxl')
response = send_request('2017-2018-regular', item).content
df = pd.read_csv(io.StringIO(response.decode('utf-8')))
df.to_excel(writer, sheets, startrow=row, startcol=column, index=False)
I would like to put a simple title at the top of my Excel sheet in considering I am working with pandas and openpyxl. How could I do such thing? I want that title could be displayed on the top of the sheet (startrow=0, startcol=0). Please show me an example how to use it.
I know the question Write dataframe to excel with a title is related, but I can't use it for the simple reason that the engine is different. I use openpyxl lib and they used xlsxwriter lib in their answer. What is the equivalent for write_string, but with pandas?
well in openpyxl first row/column start with 1 instead of 0 so row=1,column=1 will be first (0,0) top-left cell where you need to start writing
check following example.
from openpyxl import Workbook
wb = Workbook()
dest_filename = 'empty_book.xlsx'
ws1 = wb.active #first default sheet if you want to create new one use wb.create_sheet(title="xyz")
ws1.title = "Title set example"
for col in range(1, 10):
ws1.cell(column=col, row=1, value="Title_{0}".format(col))
wb.save(filename = dest_filename)
I'm new to python and I'm trying to convert some VBA used to format excel files into python. I have an excel file with 100 worksheets and I would like to delete the worksheets if they are not in a list. For example, the workbook contains worksheets 'Sheet1', 'Sheet2',...'Sheet100' I would like to delete all tabs if they are not in this list ['Sheet25', 'Sheet50', 'Sheet75', 'Sheet100'].
I'm able to use this code to delete a single worksheet:
wb = openpyxl.load_workbook('testdel.xlsx')
delete = wb.get_sheet_by_name('Sheet2')
wb.remove_sheet(delete)
wb.save('testdel2.xlsx')
I've attempted this code to delete multiple sheets / and or a single sheet but I can't seem to get it to work. Any suggestions on how to modify so that is will delete all sheets if they are not in a list? Thanks in advance for your help!
wb = openpyxl.load_workbook('testdel.xlsx')
ws = wb.get_sheet_names()
if ws is not ['Sheet25', 'Sheet50', 'Sheet75', 'Sheet100']:
wb.remove_sheet(ws)
wb.save('testdel2.xlsx')`
The problem is that you're confusing worksheet names with worksheet objects. wb.remove_sheet() needs to be passed an existing sheet. However, it's easier just to use del wb[sheetName]
wb = openpyxl.load_workbook('testdel.xlsx')
keep_sheets = ['Sheet25', 'Sheet50', 'Sheet75', 'Sheet100']
for sheetName in wb.sheetnames:
if sheetName not in keep_sheets:
del wb[sheetName]
wb.save('testdel2.xlsx')
wb = openpyxl.load_workbook('testdel.xlsx')
ws = wb.get_sheet_names()
for sheetName in ws:
if sheetName not in ['Sheet25', 'Sheet50', 'Sheet75', 'Sheet100']:
sheetToDelete = wb.get_sheet_by_name(sheetName) // gets the sheet object
wb.remove_sheet(sheetToDelete)
wb.save('testdel2.xlsx')
I have to create and write a new excel workbook with about 5 worksheets. The purpose of my code is to read a database and split that into different sheets depending on certain criterion.
I have used the following code in python 2.7 using openpyxl-1.1.0
from openpyxl.workbook import Workbook
dest_filename = 'D:\\Splitted.xlsx'
wb = Workbook()
ws1 = wb.worksheets[0]
ws1.title = 'Criterion1'
ws2 = wb.worksheets[1]
ws2.title = 'Criterion2'
## Read Database, get criterion
if criterion == Criterion1:
ws1.cell('A1').value = 'A1'
ws1.cell('B1').value = 'B1'
elif criterion == Criterion2:
ws2.cell('A1').value = 'A2'
ws2.cell('B1').value = 'B2'
wb.save(filename = dest_filename)
I am able to write single sheet, but if I try to create 2nd worksheet, I am getting an error saying "Index out of range" at code
ws2 = wb.worksheets[1]
Is there any solution to write 2 or more worksheets in a single workbook at the same time?
You shouldn't try and access worksheets by index. The error is because the second worksheet hasn't been created (every workbook has a single worksheet created automatically).
ws1 = wb.active
ws2 = wb.create_sheet()
Should solve your problem.