Insert a title at the beginning of the Excel worksheet - python

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)

Related

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

Add formats to dataframe and insert in excel using python?

i have to insert a database into excel with borders and all values in data frame should be centered i tried doing formatting to cells but does not work
df1.to_excel(writer,index=False,header=True,startrow=12,sheet_name='Sheet1')
writer.close()
writer=pd.ExcelWriter(s, engine="xlsxwriter")
writer.book = load_workbook(s)
workbooks= writer.book
worksheet = workbooks['Sheet1']
f1= workbooks.add_format()
worksheet.conditional_format(12,0,len(df1)+1,7,{'format':f1})
can u please help me with this
I'm not going to lie: I've done this for the first time right now, so this might not be a very good solution. I'm using openpyxl because it seems more flexible to me than XlsxWriter. I hope you can use it too.
My assumption is that the variable file_name contains a valid file name.
First your Pandas step:
with pd.ExcelWriter(file_name, engine='xlsxwriter') as writer:
df1.to_excel(writer, index=False, header=True, startrow=12, sheet_name='Sheet1')
Then the necessary imports from openpyxl:
from openpyxl import load_workbook
from openpyxl.styles import NamedStyle, Alignment, Border, Side
Loading the workbook and selecting the worksheet:
wb = load_workbook(file_name)
ws = wb['Sheet1']
Defining the required style:
centered_with_frame = NamedStyle('centered_with_frame')
centered_with_frame.alignment = Alignment(horizontal='center')
bd = Side(style='thin')
centered_with_frame.border = Border(left=bd, top=bd, right=bd, bottom=bd)
Selecting the relevant cells:
cells = ws[ws.cell(row=12+1, column=1).coordinate:
ws.cell(row=12+1+df1.shape[0], column=df1.shape[1]).coordinate]
Applying the defined style to the selected cells:
for row in cells:
for cell in row:
cell.style = centered_with_frame
Finally saving the workbook:
wb.save(file_name)
As I said: This might not be optimal.

How do I create a table using Openpyxl's table module?

I'm attempting to create a script to process several Excel sheets at once and one of the steps i'm trying to get Python to handle is to create a table using data passed from a pandas data frame. Creating a table seems pretty straightforward looking at the documentation.
Following the example from here:
# define a table style
mediumstyle = TableStyleInfo(name='TableStyleMedium2', showRowStripes=True)
# create a table
table = Table(displayName='IdlingReport', ref='A1:C35', tableStyleInfo=mediumstyle)
# add the table to the worksheet
sheet2.add_table(table)
# Saving the report
wb.save(openexcel.filename)
print('Report Saved')
However this creates an empty table, instead of using the data present in cells 'A1:C35'. I can't seem to find any examples anywhere that go beyond these steps so any help with what I may be doing wrong is greatly appreciated.
The data in 'A1:C35' is being written to Excel as follows:
while i < len(self.sheets):
with pd.ExcelWriter(filename, engine='openpyxl') as writer:
writer.book = excelbook
writer.sheets = dict((ws.title, ws) for ws in excelbook.worksheets)
self.df_7.to_excel(writer, self.sheets[i], index=False, header=True, startcol=0, startrow=0)
writer.save()
i += 1
The output looks something like this
Time Location Duration
1/01/2019 [-120085722,-254580042] 5 Min
1/02/2019 [-120085722,-254580042] 15 Min
1/02/2019 [-120085722,-254580042] 7 Min
Just to clarify right now I am first writing my data frame to Excel and then after formatting the data I've written as a table. Reversing these steps by creating the table first and then writing to Excel fills the table, but gets rid of the formatting(font color, font type, size, etc). Which means I'd have to add an additional step to fix the formatting(which i'd like to avoid if possible).
Your command
# create a table
table = Table(displayName='IdlingReport', ref='A1:C35', tableStyleInfo=mediumstyle)
creates a special Excel object — an empty table with the name IdlingReport.
You probably want something else - to fill a sheet of your Excel workbook with data from a Pandas dataframe.
For this purpuse there is a function dataframe_to_rows():
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
wb = Workbook()
ws = wb.active # to rename this sheet: ws.title = "some_name"
# to create a new sheet: ws = wb.create_sheet("some_name")
for row in dataframe_to_rows(df, index=True, header=True):
ws.append(row) # appends this row after a previous one
wb.save("something.xlsx")
See Working with Pandas Dataframes and Tutorial.

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)

Find range of filled contents in Excel worksheet

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)

Categories

Resources