I need to add a border around my dataframe which i have created using pandas as shown in the attached pictureExcelTableImage. Below is my code snippet. How can this be done?
[df = pd.DataFrame(data)
df2=pd.DataFrame(exportFields)
df2 = df2.transpose()
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('C:/Users/roshni.j/Desktop/Clarivoy_docs/Testing/pandas_simple.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df2.to_excel(writer, sheet_name='Sheet1',index=False, header=False,startcol=1, startrow=2)
df.to_excel(writer, sheet_name='Sheet1',index=False, header=False,startcol=1, startrow=3)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
You need to use something like StyleFrame for this (https://styleframe.readthedocs.io/en/latest/). Pure pandas can't draw any frames in Excel.
Use add_table method from pandas then you can style it.
https://xlsxwriter.readthedocs.io/working_with_tables.html
Related
I'm a super beginner and still learning Python.
I have an excel workbook which contains multiple sheets and only want certain sheets to be copied and pasted in a new created worbook and Im having some troubles.
below is my code.
import pandas as pd
import openpyxl
df = pd.read_excel('AMT.xlsb', sheet_name=['Roster','LOA'])
# print whole sheet data
with pd.ExcelWriter('output.xlsx') as writer:
df.to_excel(writer, sheet_name=['Roster','LOA'])
I get an error "IndexError: At least one sheet must be visible", none of the sheets from the AMT file are hidden.
Looks like you may be converting your frame to a dict - Try this:
import pandas as pd
import openpyxl
df = pd.read_excel('AMT.xlsb', sheet_name='Roster')
df1 = pd.read_excel('AMT.xlsb', sheet_name='LOA')
# print whole sheet data
with pd.ExcelWriter('output.xlsx') as writer:
df.to_excel(writer, sheet_name="Roster", index=False)
df1.to_excel(writer, sheet_name="LOA", index=False)
You may still have some clean up after...
Using xlsxwriter, one can write a dataframe 'df' to Excel 'simple.xlsx' using code such as:
import pandas as pd
writer = pd.ExcelWriter('simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
With above code, I see that the resultant Excel sheet has all cells (except header) as default left-aligned.
Question:
How can I make the Excel cell values to be center-aligned?
I did explore using conditional formatting but, with my cell values being combination of blanks, zeros, floats, strings and integers, I am wondering if there is another way.
Is there a smarter/quick way to do either/both of the following:
Any way to write dataframe to Excel as center-aligned? Or..
Any way to center-align the Excel sheet (for the cell range occupied by dataframe) once the dataframe has already been written to Excel?
You can add the below line to your code
df=df.style.set_properties(**{'text-align': 'center'})
Your complete code would be
import pandas as pd
writer = pd.ExcelWriter('simple.xlsx', engine='xlsxwriter')
df=df.style.set_properties(**{'text-align': 'center'})
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
This is something that has been answered and re-answered time and time again because the answer keeps changing with updates to pandas. I tried some of the solutions I found here and elsewhere online and none of them have worked for me on the current version of pandas. Does anyone know the current, March 2019, pandas 0.24.2, fix for removing the default styling that a DataFrame gives to its header when converting it to an excel sheet? Simply using xlsxwriter to overwrite the styling does not work because of an issue with precedence.
Based largely on an example provided in the Xlsxwriter Docs (link here), the fully reproducible example below removes the default pandas header format in pandas 0.24.2. Of note is that in df.to_excel(), I'm changing the header and startrow parameters.
import xlsxwriter
import pandas as pd
import numpy as np
# Creating a dataframe
df = pd.DataFrame(np.random.randn(100, 3), columns=list('ABC'))
column_list = df.columns
# Create a Pandas Excel writer using XlsxWriter engine.
writer = pd.ExcelWriter("test.xlsx", engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False, index=False)
# Get workbook and worksheet objects
workbook = writer.book
worksheet = writer.sheets['Sheet1']
for idx, val in enumerate(column_list):
worksheet.write(0, idx, val)
writer.save()
print(pd.__version__)
Expected Output:
0.24.2
Consider adjusting the header style property as a global setting:
import pandas as pd
pd.io.formats.excel.ExcelFormatter.header_style = None
...
mydataframe.to_excel(...)
The key explanation is that: pandas writes a df's header with set_cell(). A cell format (in xlsxwriter speak, a "format" is a FormatObject that you have to add to the worksheetObject) can NOT be overridden with set_row(). If you are using set_row() to your header row, it will not work, you have to use set_cell().
I just want to overwrite certain column base on my dataframe. Suppose df2 is my dataframe.
Below is the code i use. The problem is its overwrite the other columns and row even though i code it to start on columns 80.
I want its overwrite on column 80 and beyond only, but not before the column 80. 80 is index, not name.
import pandas as pd
import xlsxwriter
df2 = pd.read_excel(r'C:\Users\RUI LEONHART\Google Drive\Shop\STOCK V2.xlsx',
usecols=['XS1','S1','M1','L1','XL1','XXL1'])
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df2.to_excel(writer, sheet_name='Sheet1', startcol=80)
# Get the xlsxwriter objects from the dataframe writer object.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Close the Pandas Excel writer and output the Excel file.
writer.save()
I search around the solution. The closest one is this
python: update dataframe to existing excel sheet without overwriting contents on the same sheet and other sheets
but still overwrite the columns and row that i dont want.
I want to import the values from a Pandas dataframe into an existing Excel sheet. I want to insert the data inside the sheet without deleting what is already there in the other cells (like formulas using those datas etc).
I tried using data.to_excel like:
writer = pd.ExcelWriter(r'path\TestBook.xlsm')
data.to_excel(writer, 'Sheet1', startrow=1, startcol=11, index = False)
writer.save()
The problem is that this way i overwrite the entire sheet.
Is there a way to only add the dataframe? It would be perfect if I could also keep the format of the destination cells.
Thanks
I found a good solution for it. Xlwings natuarally supports pandas dataframe:
https://docs.xlwings.org/en/stable/datastructures.html#pandas-dataframes
The to_excel function provides a mode parameter to insert (w) of append (a) a data frame into an excel sheet, see below example:
with pd.ExcelWriter(p_file_name, mode='a') as writer:
df.to_excel(writer, sheet_name='Data', startrow=2, startcol=2)