I tried to create multiple sheets in excel using python openpyxl to store different values in loop, but it creates only one sheet.
from datetime import datetime
import openpyxl
from openpyxl.styles import Font
table_name = ["M-1", "M-2", "M-3"]
xltitle = datetime.now()
tit_head = "ALL_MACHINE" + xltitle.strftime("%d_%m_%Y_%H_%M_%S")+".xlsx"
for tab_nam in table_name:
filepath = tit_head
headings = ("NAME", "ID", "EMPLOYEE NAME",
"NUMBER", "START TIME", "STOP TIME")
wb = openpyxl.Workbook()
sheet = wb.create_sheet()
sheet.title = tab_nam
sheet.row_dimensions[1].font = Font(bold=True)
for colno, heading in enumerate(headings, start=1):
sheet.cell(row=1, column=colno).value = heading
wb.save(filepath)
You're creating a new workbook in each iteration of the loop and then saving it, overwriting the previous one. You'll want to move the workbook creation and writing the file outside the loop. You can also move the creation of headings so it doesn't need to be recreated each time.
Something like this:
filepath = tit_head
headings = ("NAME", "ID", "EMPLOYEE NAME",
"NUMBER", "START TIME", "STOP TIME")
wb = openpyxl.Workbook()
for tab_nam in table_name:
sheet = wb.create_sheet()
sheet.title = tab_nam
sheet.row_dimensions[1].font = Font(bold=True)
for colno, heading in enumerate(headings, start=1):
sheet.cell(row=1, column=colno).value = heading
wb.save(filepath)
I am trying to write to excel files using openpyxl module. For some reason it only lets me write once. If I try to write again it raises:
PermissionError: [Errno 13] Permission denied: 'expenses.xlsx'
The excel file and python program are in the same folder on D drive. What's the problem?
from openpyxl import Workbook
from openpyxl import load_workbook
from datetime import datetime
import os
class ExpenseTracker:
def __init__(self, file_name = "expenses.xlsx"):
self.fname = file_name
self.load_wb()
def load_wb(self):
"""
if the excel file doesn't exists it creates a new one
with a sheet, and calls self.col_values() which
adds values for first two columns in row 1
"""
try:
wb = load_workbook(self.fname)
except Exception:
wb = Workbook()
wb.create_sheet("Expenses", 0)
self.col_values()
wb.save(self.fname)
finally:
self.wb = wb
self.ws = self.wb["Expenses"]
def col_values(self):
# adds values for first two columns in row 1
self.ws.cell(row = 1, column = 1).value = "Date"
self.ws.cell(row = 1, column = 2).value = "Spent"
def spend_income(self, amount):
date = datetime.now()
date_formatted = date.strftime("%d.%b %Y")
last_row = self.ws.max_row + 1
last_col = self.ws.max_column + 1
self.ws.cell(row = last_row, column = 1).value = date_formatted
self.ws.cell(row = last_row, column = 2).value = amount
# writes under the last input in cols 1 and 2
self.wb.save(self.fname)
wbook = ExpenseTracker()
wbook.spend_income(5)
I am trying to create a basic excel report.
I am trying display a dataframe as well as some custom text/titles, not part of the dataframe.
However, I can only get one or the other. I don't really understand the end of the code that is needed for the dataframe to appear (workbook = writer.book and worksheet = writer.sheets['Reports'].
Here is my code:
writer = pd.ExcelWriter('reportTemplate.xlsx', engine='xlsxwriter')
workbook = xlsxwriter.Workbook('reportTemplate.xlsx')
worksheet = workbook.add_worksheet('Reports')
# REPORT TITLE
worksheet.write('D2','Daily In-Store Report')
workbook = xlsxwriter.Workbook('reportTemplate.xlsx')
worksheet = workbook.add_worksheet('Reports')
worksheet.write('D2','Daily In-Store Report')
reportTimes = ['Day','Week','Period','Quarter','Year']
cityList = ['ontario','bayshore','ottawa','limeridge','oshawa','scarborough','sherway','massonville','gatineau',
'quebec','anjou','dix30','Fairview','laval','mtltrust','stbruno','gcapitale','stefoy','rivieres','chicoutimi','sherbrooke','canada']
# LOOP THROUGH FILES
rowNb = 4
for time in reportTimes:
# TITLE
tableTitle = time + ' report as of ...'
worksheet.write('A'+str(rowNb),tableTitle)
rowNb += 1
headRow, secondHead = createHeadings(time)
worksheet.write_row('B' + str(rowNb), headRow)
worksheet.write_row('B' + str(rowNb), secondHead)
rowNb += 2
df = pd.read_csv('fy_' + time.lower() + '.csv')
df.set_index('legacy_id',inplace=True)
df = df.reindex(cityList)
print(df)
df.to_excel(writer,sheet_name='Reports',startrow = rowNb,header=False)
workbook = writer.book
worksheet = writer.sheets['Reports']
writer.save()
As the code is right now, it only displays the dataframe
It's not clear to me whether you're trying to write multiple sheets in one Excel file. If so, the problem may be that you're re-writing the same sheet called 'Reports' four times. Also, here are some basics to try. Put the df.to_excel() after pd.ExcelWriter(). Then remove from the for loop the last four lines. Finally, put writer.save() after the for loop ends. (This was not very clear for me when I first learned them, too. See more examples at this link.)
Edit: here's fully executing code (with stub data). One of of the keys was to enable multiple writes to the worksheet using writer.sheets['Reports'] = worksheet - see this explanation.
dummy_df = pd.DataFrame([[10,np.NaN],[12,42],[16,np.NaN],[20,3],[25,16],[30,1],[40,19],[60,99]],columns=['legacy_id', 'b'])
writer = pd.ExcelWriter('reportTemplate.xlsx', engine='xlsxwriter')
workbook = writer.book
worksheet = workbook.add_worksheet('Reports')
writer.sheets['Reports'] = worksheet # enable multiple writes to sheet
# REPORT TITLE
worksheet.write('D2','Daily In-Store Report')
reportTimes = ['Day','Week','Period','Quarter','Year']
cityList = ['ontario','bayshore','ottawa','limeridge','oshawa','scarborough','sherway','massonville','gatineau',
'quebec','anjou','dix30','Fairview','laval','mtltrust','stbruno','gcapitale','stefoy','rivieres','chicoutimi','sherbrooke','canada']
# LOOP THROUGH FILES
rowNb = 4
for time in reportTimes:
# TITLE
tableTitle = time + ' report as of ...'
worksheet.write('A'+str(rowNb),tableTitle)
rowNb += 1
headRow, secondHead = "dummy head row", "dummy second head" #I don't have your createHeadings(time)
worksheet.write_row('B' + str(rowNb), headRow)
worksheet.write_row('B' + str(rowNb), secondHead)
rowNb += 2
df = dummy_df.copy(deep=True) # pd.read_csv('fy_' + time.lower() + '.csv')
df.set_index('legacy_id',inplace=True)
df = df.reindex(cityList)
#print(df)
df.to_excel(writer,sheet_name='Reports', startrow = rowNb)
rowNb += df.shape[0] #gives row count
writer.save()
I am trying to create a timed backup system for my excel document with Python as multiple users will be accessing it.
I want to change the path of the file not to my local directory.
Here's the code;
import pandas as pd
import datetime
import numpy
now = datetime.datetime.now()
ct = now.strftime("%Y-%m-%d %H.%M")
table = pd.read_excel(r'Z:\new\Planner_New.xlsx',
sheet_name = 'Jan18',
header = 0,
index_col = 0,
usecols = "A:AY",
convert_float = True)
writer = pd.ExcelWriter('Planner' + ct + '.xlsx', engine='xlsxwriter')
table.to_excel(writer, sheet_name = "Jan18")
workbook = writer.book
worksheet = writer.sheets['Jan18']
format1 = workbook.add_format({'num_format': '0%'})
worksheet.set_column('H:AY', None, format1)
writer.save()
writer.close()
I have tried
outpath = (r'Z:\backup')
writer.save(outpath)
writer.close()
But get back
TypeError: save() takes 1 positional argument but 2 were given
You need to specify the save location when you create the ExcelWriter object:
writer = pd.ExcelWriter(r'Z:\backup\Planner' + ct + '.xlsx', engine='xlsxwriter')
...
writer.save()
writer.close()
I am trying to read in multiple excel files and append the data from each file into one master file. Each file will have the same headers (So I can skip the import of the first row after the initial file).
I am pretty new to both Python and the OpenPyXL module. I am able to import the first workbook without problem. My problem comes in when I need to open the subsequent file and copy the data to paste into the original worksheet.
Here is my code so far:
# Creating blank workbook
from openpyxl import Workbook
wb = Workbook()
# grab active worksheet
ws = wb.active
# Read in excel data
from openpyxl import load_workbook
wb = load_workbook('first_file.xlsx') #explicitly loading workbook, will automate later
# grab active worksheet in current workbook
ws = wb.active
#get max columns and rows
sheet = wb.get_sheet_by_name('Sheet1')
print ("Rows: ", sheet.max_row) # for debugging purposes
print ("Columns: ", sheet.max_column) # for debugging purposes
last_data_point = ws.cell(row = sheet.max_row, column = sheet.max_column).coordinate
print ("Last data point in current worksheet:", last_data_point) #for debugging purposes
#import next file and add to master
append_point = ws.cell(row = sheet.max_row + 1, column = 1).coordinate
print ("Start new data at:", append_point)
wb = load_workbook('second_file.xlsx')
sheet2 = wb.get_sheet_by_name('Sheet1')
start = ws.cell(coordinate='A2').coordinate
print("New data start: ", start)
end = ws.cell(row = sheet2.max_row, column = sheet2.max_column).coordinate
print ("New data end: ", end)
# write a value to selected cell
#sheet[append_point] = 311
#print (ws.cell(append_point).value)
#save file
wb.save('master_file.xlsx')
Thanks!
I don't really understand your code. It looks too complicated. When copying between worksheets you probably want to use ws.rows.
wb1 = load_workbook('master.xlsx')
ws2 = wb1.active
for f in files:
wb2 = load_workbook(f)
ws2 = wb2['Sheet1']
for row in ws2.rows[1:]:
ws1.append((cell.value for cell in row))