Change column width of existing tab in excel sheet - python

I have dataframe (df) that is added to an existing excel file as a new tab ('Print'). I am having difficulties adjusting the column width any ideas?
Code
book = load_workbook('file.xlsx')
writer = pd.ExcelWriter('file.xlsx', engine = 'openpyxl')
writer.book = book
df.to_excel(writer,sheet_name = 'Print')
worksheet = writer.sheets['Print']
worksheet.set_column('B:B', 40) #This does not work

writer = pd.ExcelWriter('file.xlsx', engine='openpyxl')
writer.book = book
df.to_excel(writer, sheet_name='Print')
sheet = book.get_sheet_by_name('Print')
sheet.column_dimensions['B'].width = 40
writer.save()

I've never used nor done this, but I just googled for "worksheet.set_column" and found this:
https://xlsxwriter.readthedocs.io/worksheet.html
The syntax for the function is
set_column(first_col, last_col, width, cell_format, options)
So I'd say the answer is:
worksheet.set_column(2, 2, 40)

Related

Create Excel Tables from Dictionary of Dataframes

I have dictionary of dataframes.
dd = {
'table': pd.DataFrame({'Name':['Banana'], 'color':['Yellow'], 'type':'Fruit'}),
'another_table':pd.DataFrame({'city':['Atlanta'],'state':['Georgia'], 'Country':['United States']}),
'and_another_table':pd.DataFrame({'firstname':['John'], 'middlename':['Patrick'], 'lastnme':['Snow']}),
}
I would like to create an Excel file which contains Excel Table objects created from these dataframes. Each Table needs to be on a separate Tab/Sheet and Table names should match dataframe names.
Is this possible to do with Python?
So far I was only able to export data to Excel normally without converting to tables using xlsxwriter
writer = pd.ExcelWriter('Results.xlsx', engine='xlsxwriter')
for sheet, frame in dd.items():
frame.to_excel(writer, sheet_name = sheet)
writer.save()
For writing multiple sheets from Pandas, use the openpyxl library. In addition, to prevent overwriting, set the workbook sheets before each update.
Try this code:
import pandas as pd
import openpyxl
dd = {
'table': pd.DataFrame({'Name':['Banana'], 'color':['Yellow'], 'type':'Fruit'}),
'another_table':pd.DataFrame({'city':['Atlanta'],'state':['Georgia'], 'Country':['United States']}),
'and_another_table':pd.DataFrame({'firstname':['John'], 'middlename':['Patrick'], 'lastnme':['Snow']}),
}
filename = 'Results.xlsx' # must exist
wb = openpyxl.load_workbook(filename)
writer = pd.ExcelWriter(filename, engine='openpyxl')
for sheet, frame in dd.items():
writer.sheets = dict((ws.title, ws) for ws in wb.worksheets) # need this to prevent overwrite
frame.to_excel(writer, index=False, sheet_name = sheet)
writer.save()
# convert data to tables
wb = openpyxl.load_workbook(filename)
for ws in wb.worksheets:
mxrow = ws.max_row
mxcol = ws.max_column
tab = openpyxl.worksheet.table.Table(displayName=ws.title, ref="A1:" + ws.cell(mxrow,mxcol).coordinate)
ws.add_table(tab)
wb.save(filename)
Output

Insert Button to Spreadsheet using df.to_excel

I'm trying to insert a button into a spreadsheet, but I'm not able to use insert_button properly.
What I did so far :
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
workbook = writer.book
df_tst.to_excel(writer, sheet_name='Info' ,index = False , header = False)
workbook.add_vba_project(r'C:\Users\...\Project.bin')
workbook.filename = 'test.xlsm'
writer.save()
But I'm not able to use insert_button to the spreasheet " Info " ( probably because I'm trying the wrong way... )
Then I tried a different option that so far it works as expected, but what I'm trying to do is to insert a button just like the following :
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
workbook = writer.book
worksheet1 = workbook.add_worksheet()
worksheet1.write('A1', 'TEST.')
worksheet1.insert_button('C6', {'macro': 'macro_test',
'caption': 'Macro Test',
'width': 100,
'height': 80})
workbook.add_vba_project(r'C:\Users\...\Project.bin')
workbook.filename = 'test.xlsm'
writer.save()
The problem is that using " worksheet1 = workbook.add_worksheet() ", I'm not able to insert the dataframe to the sheet, giving the following error when I tried :
worksheet1.write(df_tst)
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
worksheet1.write(df_tst)
File "C:\Users\...\worksheet.py", line 63, in cell_wrapper
int(first_arg)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'DataFrame'
So either inserting the dataframe content into the worksheet created using "workbook.add_worksheet()" or using the "insert_button" to the spreadsheet created by df.to_excel would solve the problem.
Thanks in advance
Here is a working example with Pandas and XlsxWriter. See also Working with Python Pandas and XlsxWriter in the XlsxWriter docs.
import os
import pandas as pd
import xlsxwriter
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
# 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.
df.to_excel(writer, sheet_name='Sheet1')
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
worksheet.set_column('D:D', 30)
# Add the VBA project binary.
workbook.add_vba_project('./vbaProject.bin')
# Show text for the end user.
worksheet.write('D3', 'Press the button to say hello.')
# Add a button tied to a macro in the VBA project.
worksheet.insert_button('D5', {'macro': 'say_hello',
'caption': 'Press Me',
'width': 80,
'height': 30})
# Close the Pandas Excel writer and output the Excel file.
writer.save()
# Pandas doesn't allow a '.xslm' extension but Excel requires
# it for files containing macros so we rename the file.
os.rename('pandas_simple.xlsx', 'pandas_simple.xlsm')
Output:
Try to use xlsxwriter to open directly the file, not pandas wrapper
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet1 = workbook.add_worksheet()
worksheet1.write('A1', 'TEST.')
worksheet1.insert_button('C6', {'macro': 'macro_test',
'caption': 'Macro Test',
'width': 100,
'height': 80})
workbook.add_vba_project(r'C:\Users\...\Project.bin')
workbook.filename = 'test.xlsm'
writer.save()

AttributeError when trying to save pandas dataframe to existing excel sheet

I am trying to write a pandas data frame to an existing excel sheet on a new tab, but it gives me the following error:
AttributeError: 'NoneType' object has no attribute 'read'.
I've determined this is because pandas to_excel returns a NoneType object, which isn't allowing me to save the file with writer.save(). Does anyone know a workaround for this?
path = 'summary.xlsx'
book = load_workbook(path)
writer = pd.ExcelWriter(path, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, sheet_name="results")
writer.save()
I had exactly the same issue.
I managed to work around it by removing the value in legacy_drawing from each sheet in the workbook.
path = 'summary.xlsx'
book = load_workbook(path)
writer = pd.ExcelWriter(path, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
for s in list(writer.sheets.keys()):
writer.sheets[s].legacy_drawing = None
df.to_excel(writer, sheet_name="results")
writer.save()

How to write at the same time to Different excel Using Python

I try to write to all files, that I have at the same time.
I have some files
izzymonroe#mail.ru.xlsx,
lucky-frog#mail.ru.xlsx,
lucky-frog#mail.ru.xlsx,
izzymonroe#mail.ru.xlsx,
Yubodrova#ya.ru.xlsx,
lucky-frog#mail.ru.xlsx,
Ant.karpoff2011#yandex.ru.xlsx
9rooney9#list.ru.xlsx
and I want to write data to this. But how can I send it to function(and I need to write to file value with groupby)
df = pd.read_excel('group.xlsx')
def add_xlsx_sheet(df, sheet_name=u'Смартфоны полно', index=True, digits=1, path='9rooney9#list.ru.xlsx'):
book = load_workbook(path)
writer = ExcelWriter('9rooney9#list.ru.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
if sheet_name in list(writer.sheets.keys()):
sh = book.get_sheet_by_name(sheet_name)
book.remove_sheet(sh)
df.to_excel(writer, sheet_name=u'Смартфоны полно', startrow=0, startcol=0,
float_format='%.{}f'.format(digits), index=index)
writer.save()
It works to one file, but it write all data to this file. But I need to write group, where id in mail complies the name of file
How can I specify all file in function and next
df.groupby('member_id').apply(lambda g: g.to_excel(str(g.name) + '.xlsx', 'sheet2'))
The problem was solved with df.groupby('col_name').apply(lambda x: add_xlsx_sheet(x, x.name, path='{}.xlsx'.format(x.name)))

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

Categories