Python openpyxl switch between spreadsheets in one workbook - python

I can see all the sheet names when i use
wb.get_sheet_names()
now i would like to rename all the sheets one after the other to numbers i.e first sheet to 1, second sheet to 2 and so on.

Loop through each sheet and set the title to whatever you want.
from openpyxl import load_workbook
wb = load_workbook('test.xlsx')
sheets = wb.get_sheet_names()
counter = 0
for sheet in sheets:
ss_sheet = wb.get_sheet_by_name(sheet)
ss_sheet.title = str(counter)
wb.save("file.xlsx")
counter+= 1

from openpyxl import load_workbook
work_book = load_workbook('test.xlsx')
sheets = work_book.get_sheet_names()
name = ['Name1', 'Name2', 'Name3']
index = 0
for sheet in sheets:
sheetname = work_book.get_sheet_by_name(sheet)
sheetname.title = str(name[index])
work_book.save("renamed.xlsx")
index += 1

Related

Append data into new excel sheet with openpyxl

I have bunch of excel workbooks and I would like to get cell values from them and write to a new sheet.
My code is not appending new data.It is just overwriting cells with values from last workbook.
(I've changed the pasted code.It was pasted incorrect.)
Here is my code
from openpyxl import load_workbook
booklist = ["17_02.xlsx", "17_03.xlsx",
"17_04.xlsx", "17_05.xlsx",
"17_06.xlsx", "17_08.xlsx",
"17_09.xlsx", "17_10.xlsx"]
for wb in booklist:
book = load_workbook(filename =wb,data_only=True)
report = load_workbook(filename="dest.xlsx", data_only=True)
print(book)
sheet = book['Sheet']
reportsheet=report['First']
row_count=sheet.max_row
column_count=sheet.max_column
for r in range(1,row_count+1):
for c in range(1,column_count+1):
source=sheet.cell(row=r, column=c)
dest = reportsheet.cell(row=r, column=c)
dest.value = source.value
sheet.title = 'First'
book.save("dest.xlsx")
Edit:
After the mickNeill's answer I changed the code and it worked for appending.But now there is another problem.
If I run the code (after clearing the cells) second time or more it's appending the data to the rows after the cleared cells.
First run:
Data appended to A1:A20
Clear the cells,save and close the workbook.
Second run:
Data appended to A21:A20 instead of A1:A20 (cleared cells)
Every time I run the code value of the reportRow continues to increase (1,20,40 ...) and appending data to higher number of rows.
from openpyxl import load_workbook
booklist = ["17_02.xlsx", "17_03.xlsx",
"17_04.xlsx", "17_05.xlsx",
"17_06.xlsx", "17_08.xlsx",
"17_09.xlsx", "17_10.xlsx"]
for wb in booklist:
book = load_workbook(filename =wb,data_only=True)
report = load_workbook(filename="dest.xlsx", data_only=True)
print(book)
sheet = book['Sheet']
reportsheet=report['First']
row_count=sheet.max_row
reportRow = reportsheet.max_row
column_count=sheet.max_column
for r in range(1,row_count+2):
for c in range(1,column_count+1):
source=sheet.cell(row=r, column=c)
dest = reportsheet.cell(row=reportRow, column=c)
dest.value = source.value
reportRow += 1
report.save("dest.xlsx")
Try this: Editied, you are saving the wrong book, last line
from openpyxl import load_workbook
booklist = ["Book5.xlsx", "Book6.xlsx","Book7.xlsx"]
report = load_workbook(filename="dest.xlsx", data_only=True)
for wb in booklist:
book = load_workbook(filename =wb,data_only=True)
#print(book)
sheet = book['Sheet1']
reportsheet=report['First']
row_count=sheet.max_row
reportRow = reportsheet.max_row + 1
print reportRow
column_count=sheet.max_column
for r in range(1,row_count+1):
for c in range(1,column_count+1):
print reportRow
source=sheet.cell(row=r, column=c)
dest = reportsheet.cell(row=reportRow, column=c)
dest.value = source.value
sheet.title = 'First'
reportRow += 1
report.save("dest.xlsx")

How to read specific sheets from My XLS file in Python

As of now i can read EXCEL file's all sheet.
e.msgbox("select Excel File")
updated_deleted_xls = e.fileopenbox()
book = xlrd.open_workbook(updated_deleted_xls, formatting_info=True)
openfile = e.fileopenbox()
for sheet in book.sheets():
for row in range(sheet.nrows):
for col in range(sheet.ncols):
thecell = sheet.cell(row, 0)
xfx = sheet.cell_xf_index(row, 0)
xf = book.xf_list[xfx]
If you open your editor from the desktop or command line, you would have to specify the file path while trying to read the file:
import pandas as pd
df = pd.read_excel(r'File path', sheet_name='Sheet name')
Alternatively, if you open your editor in the file's directory, then you could read directly using the panda library
import pandas as pd
df = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='Title Sheet')
df1 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx',sheet_name='Transactions')
df2 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='NewCustomerList')
df3 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='CustomerDemographic')
df4 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='CustomerAddress')
Maybe Pandaswould be helpful ( the go-to package for data) :
import pandas as pd
df = pd.read_excel('filname.xls', sheet = 0)
Edit: Since a lot of time has passed and pandas matured the arguemnts have change. So for pandas >1.0.0
import pandas as pd
df = pd.read_excel('filname.xls', sheet_name = 0)
You can use book.sheet_by_name() to read specific sheets by their name from xls file.
for name, sheet_name in zip(filename, sheetnumber):
book = xlrd.open_workbook(name)
sheet = book.sheet_by_name(sheet_name)
for row in range(sheet.nrows):
for column in range(sheet.ncols):
thecell = sheet.cell(row, 0)
xfx = sheet.cell_xf_index(row, 0)
xf = book.xf_list[xfx]
filename is the path to your xls file. Specify the sheet number you need to read in sheetnumber.
Alternatively, you could use book.sheet_by_index() and pass argument to return a specific sheet.
From docs:
sheet_by_index(sheetx)
Parameters: sheetx – Sheet index in range(nsheets)
For example:
first_sheet = book.sheet_by_index(0) # returns the first sheet.
You can use either book.sheet_by_name() or book.get_sheet()
Example using get_sheet()
book = xlrd.open_workbook(updated_deleted_xls, formatting_info=True)
sheet = book.get_sheet(0) #Gets the first sheet.
Example using sheet_by_name()
book = xlrd.open_workbook(updated_deleted_xls, formatting_info=True)
sheet_names = book.sheet_names()
xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])
MoreInfo on getting sheet by sheet_by_name

How to write multiple sheets into a new excel, without overwriting each other?

I'm trying to write multiple excels' column A into a new excel's column A (assuming all the excels have one worksheet each.) I've written some code, which can write one excel's column A into the new excel's column A; but if there are multiple excels, the new excel's column A will be overwritten multiple times. So how could I just add all the column As to the new excel sheet one after another without overwriting each other?
Below are my code:
import os, openpyxl
path = os.getcwd()
def func(file):
for file in os.listdir(path):
if file.endswith('.xlsx'):
wb = openpyxl.load_workbook(file)
sheet = wb.active
colA = sheet['A']
wb = openpyxl.Workbook()
r = 1
for i in colA:
sheet = wb.active
sheet.cell(row=r, column=1).value = i.value
r += 1
wb.save('new.xlsx')
func(file)
Thank you so much!!
you could proceed for example as:
import os, openpyxl
path = os.getcwd()
def func(outputFile):
c = 0
#create output workbook
wbOut = openpyxl.Workbook()
sheetOut = wbOut.active
for fName in os.listdir(path):
if fName.endswith('.xlsx'):
c += 1 #move to the next column in output
wb = openpyxl.load_workbook(fName)
sheet = wb.active #input sheet
#for r in range(1, sheet.max_row+1):
# sheetOut.cell(row=r, column=c).value = sheet.cell(row = r, column = 1).value
for r, cell in enumerate(sheet['A']):
sheetOut.cell(row = r+1, column = c).value = cell.value
wbOut.save(outputFile)
#"concatenate" all columns A into one single column
def funcAppend(outputFile):
wbOut = openpyxl.Workbook()
sheetOut = wbOut.active
r = 1
for fName in os.listdir(path):
if fName.endswith('.xlsx'):
wb = openpyxl.load_workbook(fName)
sheet = wb.active
for cell in sheet['A']:
sheetOut.cell(row = r, column = 1).value = cell.value
r += 1
wbOut.save(outputFile)
func('test.xlsx')

Openpyxl - how to populate data on specific sheet

I'm using openpyxl for the first time. I have to read excel file, then after manipulation, populate the result on three different excel sheets -> sheet_T, sheet_D and sheet_U. I created three sheets using openpyxl as follows-
sheet_T = filename2.create_sheet(0)
sheet_T.title = "Target First"
sheet_D = filename2.create_sheet(1)
sheet_D.title = "Distractor First"
sheet_U = filename2.create_sheet(2)
sheet_U.title = "Unclassified"
I used xlwt to do it but there is a constraint of 256 columns. Hence, I used openpyxl. The below code is written by using xlwt-
sheet_T.write(row_first, col_target, Name_Target)
sheet_D.write(row_first, col_target, Name_Target)
sheet_U.write(row_first, col_target, Name_Target)
How do I write the same thing by using openpyxl? All the documentation I read is how to write on a specific cell not sheet.
Many thanks for the help!
You need to create another sheet:
from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter
wb = Workbook()
ws0 = wb.worksheets[0]
ws0.title = 'My Sheet 1'
ws1 = wb.create_sheet()
ws1.title = 'My Sheet 2'
ws2 = wb.create_sheet()
ws2.title = 'My Sheet 3'
Now you can write to the different sheets:
cell_ws0_a1 = ws0.cell('A1')
cell_ws0_a1.value = 'Wrote to cell in 1st sheet.'
cell_ws1_a1 = ws1.cell('A1')
cell_ws1_a1.value = 'Wrote to cell in 2nd sheet.'
cell_ws2_a1 = ws2.cell('A1')
cell_ws2_a1.value = 'Wrote to cell in 3rd sheet.'
writer = ExcelWriter(workbook=wb)
writer.save('example.xlsx')
There is only one sheet in a workbook by default. wb.create_sheet() creates a second sheet.
I would like to make a correction in the code for smooth running-
from openpyxl.workbook import Workbook
from openpyxl.writer.excel import ExcelWriter
wb = Workbook()
ws0 = wb.worksheets[0]
ws0.title = 'My Sheet 1'
ws1 = wb.create_sheet()
ws1.title = 'My Sheet 2'
ws2 = wb.create_sheet()
ws2.title = 'My Sheet 3
v1 = ws1.cell(row=1, column=1)
v1.value = "Hello"
(Basically insert values in this manner instead)
And then end with-
writer = ExcelWriter(wb, 'file.xlsx')
wb.save('file.xlsx')
Hope this helps :)

Activate second worksheet with openpyxl

I am trying to activate multiple excel worksheets and write to both multiple sheets within both workbook(s) using python and openpyxl. I am able to load the second workbook f but I am unable to append cell G2 of my second workbook with the string Recon
from openpyxl import Workbook, load_workbook
filename = 'sda_2015.xlsx'
wb = Workbook()
ws = wb.active
ws['G1'] = 'Path'
ws.title = 'Main'
adf = "Dirty Securities 04222015.xlsx"
f = "F:\\ana\\xlmacro\\" + adf
wb2 = load_workbook(f)
"""
wb22 = Workbook(wb2)
ws = wb22.active
ws['G1'] = "Recon2"
ws.title = 'Main2'
"""
print wb2.get_sheet_names()
wb.save(filename)
I commented out the code which is broken
Update
I adjusted my code with the below answer. The value in cell H1 is written onto wb2 in column H, but for some reason the column is hidden. I have adjusted the column to other columns but still I have seen the code hide multiple columns. There are also occurences when the code executes and titles ws2 as Main21 but the encoded value is Main2
from openpyxl import Workbook, load_workbook
filename = 'sda_2015.xlsx'
wb1 = Workbook()
ws1 = wb1.active
ws1['G1'] = 'Path'
ws1.title = 'Main'
adf = "Dirty Securities 04222015.xlsx"
f = "F:\\ana\\xlmacro\\" + adf
wb2 = load_workbook(f)
ws2 = wb2.active
ws2['H1'] = 'Recon2'
ws2.title = 'Main2'
print wb2.get_sheet_names()
wb1.save(filename)
wb2.save(f)
If you have two workbooks open, wb1 and wb2, you'll also need different names for the various worksheets: ws1 = wb1.active and ws2 = wb2.active.
If you're working with a file with macros, you'll need to set the keep_vba flag to True when opening it in order to preserve the macros.
I had experienced the same thing with hidden cells. Eventually, I unpacked the Excel file and looked at the raw XML to find out that not all of the columns had a dimension for width. Those without a width were being by Excel.
A quick fix is to do something like this...
for col in 'ABCDEFG':
if not worksheet.column_dimensions[col].width:
worksheet.column_dimensions[col].width = 10

Categories