Find range of filled contents in Excel worksheet - python

I have an Excel 2016 Book.xlsm. In the worksheet testsheet, the cells in the range A1:Y150 are filled with text or number contents. The upper-left cell is always A1.
I am using python v3 xlwings to open the Excel file.
import xlwings as xw
Book_name = 'C:/Users/name/Book.xlsm'
sheet_name = 'testsheet'
wb = xw.Book(Book_name)
sht = wb.sheets[sheet_name]
How do I find out the range of cells that are filled with contents using python, which in this case is A1:Y150?

You can get the range filled with contents with used_range:
import xlwings as xw
filename = "test.xlsx"
wb = xw.Book(filename)
ws = wb.sheets["SheetX"]
a_range = ws.used_range.address
print(a_range)

If wb is defined as Excel Workbook, then this is a good way:
print (wb.sheets[sheet_name].api.UsedRange.Address)

Related

How to write pandas.DataFrame data with array values back to excel

Pandas.DataFrame, I have this output data as a dataframe and i wanted to write back this data back to excel.
This is excel sheet format
I wanted to write dataframe row in excel cell, for example :- Kosten EK will goes in excel sheet D4, IRR mit Finanzierung will go in excel sheet D5. I have same dataframe in which Soll-SOC 1-12 value is single value not an array and it is working properly, but for this case because of array i could not write. how can i solve this?
I am using xlwings, xlwriter to write data back to excel
import xlwings as xw
wb = xw.Book(file_path) # wb = xw.Book(filename) would open an existing file
Working_Sheet = wb.sheets["sheet_name"] # activating working sheet
Working_Sheet.range('D4:D15').options(index=False,header=False).value = Data[20000][0.25]
You should try to convert to a pd.DataFrame object.
import pandas as pd
import xlwings as xw
df = pd.DataFrame(...)
import xlwings as xw
wb = xw.Book(file_path) # wb = xw.Book(filename) would open an existing file
Working_Sheet = wb.sheets["sheet_name"] # activating working sheet
Working_Sheet.range('D4:D15').options(convert=pd.DataFrame, index=False,header=False).value = Data[20000][0.25]

Tuple TypeError when assigning Value to Cell in openpyxl

I'm a first time user of openpyxl and am struggling with basic Cell editing.
In the following code I'm trying to change the Cell Value "B3" in my Excel File to a String Value (eg. "Germany").
What I don't seem to understand is why the openpyxl Worksheet & Worbook are an immutable tuple type and the Documentation is suggesting the simple Cell Assignment I'm using.
Here's my Code:
from openpyxl import Workbook
from openpyxl import load_workbook
# 1. Open Excel File
wb = Workbook()
wb = load_workbook(filename="myFile.xlsm", read_only=True, keep_vba=True, data_only=True)
ws = wb.active[1] # ws is the second Worksheet in the Workbook
# 2. Enter Input (Country)
ws["B3"] = "Germany"
wb.save("myFile.xlsm")
ws["B3"] = "Germany"
TypeError: 'tuple' object does not support item assignment
Expectation
I expected to find a Excel file that contains my assigned value in the given cell.
I appreciate every answer - thanks!
Line ws = wb.active[1] is probably wrong. Also you should use read_only=False (simply remove this param, the default is False) if you want to modify the file.
You can assign ws by name. If you don't know the names you can list them like this:
>>> wb.sheetnames
['Sheet1', 'Sheet2', 'Sheet3']
>>> ws = wb['Sheet2'] # or ws = wb.active - "Get the currently active sheet"
>>> ws["B3"] = "Germany"
Whole code:
from openpyxl import Workbook
from openpyxl import load_workbook
# 1. Open Excel File
wb = Workbook()
wb = load_workbook(filename="myFile.xlsm", keep_vba=True, data_only=True)
ws = wb.active # the currently active sheet
# 2. Enter Input (Country)
ws["B3"] = "Germany"
wb.save("myFile.xlsm")

how to save the new excel with the same style in every cell using openpyxl

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

why openpyxl creating worksheet with name"Sheet1"?

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)

Insert a title at the beginning of the Excel worksheet

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)

Categories

Resources