Read Excel Sheet based on VBA CodeName property - python

I have a .xls Excel file and know the VBA CodeName property for the sheet I want to read. How can I read the sheet into Python by using the Sheet's CodeName property, rather than it's Name property?
Topic discussing the difference between VBA CodeName and Name: Excel tab sheet names vs. Visual Basic sheet names

#you have to install the module
#import the xlrd module
import xlrd
workbook = xlrd.open("example.xls") #it can take x|xs|xls format
#if you know the name of the sheet use
sheet = workbook.sheet_by_name("name of the sheet")
#or you can use index number
sheet = workbook.sheet_by_index("index of the sheet here")
#you can print the cell you want like this
print(" print from the 4th row 2nd cell".format(sheet(4,2).value))
if this was helpful give it a like if not i don't understand english well thankyou and i am new

Related

how to add the worksheet object in openpyxl to the new sheet of the existing xlsx document

I have done a lot of research and failed to solve this problem, so I am coming here to ask for help. I read the worksheet object from a file below,
sheet_2_workbook = openpyxl. load_workbook(sheet_2_path)
sheet_2 = sheet_2_workbook.worksheets\[0\]
As described in the title, I want to add it to the new sheet of the existing .xlsx document, how should I do it?
I tried to realize this as below, but the new document obtained by this method will lose some of the original formatting, including the cell background color and merged cells
old_wb = openpyxl. load_workbook(file_list[i])
old_sheet_name = old_wb. get_sheet_names()[0]
old_ws = old_wb[old_sheet_name]
ws2 = combined_wb.create_sheet(sheet_name)
for row in old_ws.values:
ws2.append(row)
I am sure that the worksheet object read in the file contains these formats, because the .xlsx document I dumped with the following code has the format mentioned above
sheet_2_workbook. save(filename = temp_save_path)
How I would go about your task of adding a worksheet object to a new sheet in an existing Excel document using openpyxl:
Import the openpyxl module
Load the existing Excel document using the load_workbook() function
Create a new sheet using the create_sheet() method of the Workbook object
Alternatively, you can specify the name of the new sheet as a string when calling the create_sheet() method
Save changes using the save() method
(To preserve the formatting you can use the copy_worksheet() to create a copy of the original worksheet and add it to the workbook)
import openpyxl
workbook = openpyxl.load_workbook('existing_document.xlsx')
sheet_2_workbook = openpyxl.load_workbook(sheet_2_path)
sheet_2 = sheet_2_workbook.worksheets[0]
new_sheet = workbook.copy_worksheet(sheet_2)
// Alternatively, you can specify the name of the new sheet as a string
// new_sheet = workbook.copy_worksheet(sheet_2, 'Sheet2')
workbook.save('existing_document.xlsx')

How python XLRD library scans all sheets in excel?

I am planning to use XLRD libraries for reading the number of rows and columns in the excel file that I imported.
I use following codes which work perfectly fine.
import xlrd
path = 'sample123.xlsx'
inputWorkbook = xlrd.open_workbook(path)
inputWorksheet = inputWorkbook.sheet_by_index(0)
print("Your worksheet has: " + str(inputWorksheet.nrows) + " rows")
print("Your worksheet has: " + str(inputWorksheet.ncols) + " columns")
However, that codes only run for a sheet (the first one). If I would like to randomly import a number of excel files that I do not know the total index or sheet names of each file, is there any coding suggestion so that all sheets in that file could be scanned through, thus the number of rows and columns for all sheets can be detected?
Thanks very much for your assistance.
However, that codes only run for a sheet (the first one)
that is because you are passing the index=0 when calling get sheet method...
you call the method get_sheet
myDoc.get_sheet(index)
where index is the index of the sheet, if you dont know it, you can find it by name:
sheet_names().index(nameOfMySheet)
here the doc
here is an example about how to get the sheets in a workbook
import xlrd
book = xlrd.open_workbook("sample.xls")
for sheet in book.sheets():
print sheet.name
To read all sheets from one excel file by using xlrd,
import xlrd
path = 'sample123.xlsx'
inputWorkbook = xlrd.open_workbook(path)
dict_sheet_tabs= {} # Store sheets in a dictionary
for sheet_name in inputWorkbook.sheet_names():
print(sheet_name ) # name of each tab
all_sheet = wb1.sheet_by_name(sheet_name) # read sheet by name
dict_sheet_tabs.update({sheet_name:all_sheet })
print(dict_sheet_tabs)
>>> {'sheet_name1': <xlrd.sheet.Sheet object at 0x7fa903b6efd0>, 'sheet_name2': <xlrd.sheet.Sheet object at 0x7fa9038ece10>}
#The dictionary keys are sheet names and values are the sheet content

Trim Sheet Name while read_excel in Pandas

I am trying to read excel with multiple sheets. I am able to read most of the files and their sheet names quiet correctly. However, some sheet names have a blank trailing space either before or after.
# I know the sheet name
party_str="Party Details"
# Reading that Sheet name
sheets['df_Party'] = pd.read_excel(open(data_file,'rb'), party_str)
**Error:**
XLRDError: No sheet named <'Party Details'>
Extra space after Details
Is there any pythonic way to handle this?
Use xlrd to get your sheet names of the file if that's convenient for you:
import xlrd
xls = xlrd.open_workbook('your_xlsx_file.xlsx')
print(xls.sheet_names())

xlsxwriter: is there a way to open an existing worksheet in my workbook?

I'm able to open my pre-existing workbook, but I don't see any way to open pre-existing worksheets within that workbook. Is there any way to do this?
You cannot append to an existing xlsx file with xlsxwriter.
There is a module called openpyxl which allows you to read and write to preexisting excel file, but I am sure that the method to do so involves reading from the excel file, storing all the information somehow (database or arrays), and then rewriting when you call workbook.close() which will then write all of the information to your xlsx file.
Similarly, you can use a method of your own to "append" to xlsx documents. I recently had to append to a xlsx file because I had a lot of different tests in which I had GPS data coming in to a main worksheet, and then I had to append a new sheet each time a test started as well. The only way I could get around this without openpyxl was to read the excel file with xlrd and then run through the rows and columns...
i.e.
cells = []
for row in range(sheet.nrows):
cells.append([])
for col in range(sheet.ncols):
cells[row].append(workbook.cell(row, col).value)
You don't need arrays, though. For example, this works perfectly fine:
import xlrd
import xlsxwriter
from os.path import expanduser
home = expanduser("~")
# this writes test data to an excel file
wb = xlsxwriter.Workbook("{}/Desktop/test.xlsx".format(home))
sheet1 = wb.add_worksheet()
for row in range(10):
for col in range(20):
sheet1.write(row, col, "test ({}, {})".format(row, col))
wb.close()
# open the file for reading
wbRD = xlrd.open_workbook("{}/Desktop/test.xlsx".format(home))
sheets = wbRD.sheets()
# open the same file for writing (just don't write yet)
wb = xlsxwriter.Workbook("{}/Desktop/test.xlsx".format(home))
# run through the sheets and store sheets in workbook
# this still doesn't write to the file yet
for sheet in sheets: # write data from old file
newSheet = wb.add_worksheet(sheet.name)
for row in range(sheet.nrows):
for col in range(sheet.ncols):
newSheet.write(row, col, sheet.cell(row, col).value)
for row in range(10, 20): # write NEW data
for col in range(20):
newSheet.write(row, col, "test ({}, {})".format(row, col))
wb.close() # THIS writes
However, I found that it was easier to read the data and store into a 2-dimensional array because I was manipulating the data and was receiving input over and over again and did not want to write to the excel file until it the test was over (which you could just as easily do with xlsxwriter since that is probably what they do anyway until you call .close()).
After searching a bit about the method to open the existing sheet in xlxs, I discovered
existingWorksheet = wb.get_worksheet_by_name('Your Worksheet name goes here...')
existingWorksheet.write_row(0,0,'xyz')
You can now append/write any data to the open worksheet.
You can use the workbook.get_worksheet_by_name() feature:
https://xlsxwriter.readthedocs.io/workbook.html#get_worksheet_by_name
According to https://xlsxwriter.readthedocs.io/changes.html the feature has been added on May 13, 2016.
"Release 0.8.7 - May 13 2016
-Fix for issue when inserting read-only images on Windows. Issue #352.
-Added get_worksheet_by_name() method to allow the retrieval of a worksheet from a workbook via its name.
-Fixed issue where internal file creation and modification dates were in the local timezone instead of UTC."
Although it is mentioned in the last two answers with it's documentation link, and from the documentation it seems indeed there are new methods to work with the "worksheets", I couldn't able to find this methods in the latest package of "xlsxwriter==3.0.3"
"xlrd" has removed support for anything other than xls files now.
Hence I was able to workout with "openpyxl" this gives you the expected functionality as mentioned in the first answer above.

How to add new column and row to .xls file using xlrd

How do you add a new column and/or row to a sheet in xlrd?
I have a .xls file that I read using open_workbook() and I need to add a new column("bouncebacks") to the first sheet then new rows to that sheet but I cannot find any functions in the xlrd documentation that shows how to add new rows and/or columns?
If I cant add a row/column in xlrd is there another way/library that allows me to add a row or column to an .xls file?
Can you show me how I can add a row and column to a sheet?
import xlrd
book = xlrd.open_workbook("abc.xls")
sheet = book.sheet_by_index(0)
# how do I add a new column("bouncebacks") to the sheet?
# how do I add a new row to the sheet?
xlrd reads xls files. xlwt creates new xls files. You need xlutils. Read this. Work through the tutorial that you'll find mentioned there.
xlrd is for reading from an .xls file. for writting to it use xlwt.

Categories

Resources