how to create a new xlsx file using openpyxl? - python

Does anybody knows how to create a new xlsx file using openpyxl in python?
filepath = "/home/ubun/Desktop/stocksinfo/yyy.xlsx"
wb = openpyxl.load_workbook(filepath)
ws = wb.get_active_sheet()
What do I need to add?

I'm sorry my head is turning around. :)
This solves the problem
filepath = "/home/ubun/Desktop/stocksinfo/test101.xlsx"
wb = openpyxl.Workbook()
wb.save(filepath)
This will create a new file.
Documentation site contains quickstart on front page.

Install and Import openpyxl
import openpyxl
Create new workbook
wb = openpyxl.Workbook()
Get SHEET name
Sheet_name = wb.sheetnames
Save created workbook at same path where .py file exist
wb.save(filename='Test.xlsx')

This is covered in the documentation: https://openpyxl.readthedocs.io/en/stable/tutorial.html#saving-to-a-file
wb.save(…)

Related

How to preserve chart formatting and textbox in existing excel files using Openpyxl in Python?

I am using Openpyxl to add some data to an existing excel file but unfortunately it also changes the format of my chart (border, background and curves colors) and deletes textbox.
Does anyone know how to prevent these changes ?
Thanks ahead !
A simplified version of my code below and
a screenshot of my excel file
so everyone can reproduce the excel file.
import os
import sys
import pandas as pd
from openpyxl import load_workbook
folder = r'C:\MyFolder'
filename = r'test.xlsx'
writer = pd.ExcelWriter(os.path.join(folder, filename),
engine='openpyxl',
datetime_format='dd/mm/yyyy',
date_format='dd/mm/yyyy')
writer.book = load_workbook(os.path.join(folder, filename))
writer.sheets = {ws.title:ws for ws in writer.book.worksheets}
writer.save()

How write to xlsm using openpyxl

I am writing this piece of code. My objective is to set a value in a specific cell of this excel file. The code is running quite ok and exits with no error, but the cell A1 remains blank. How can I fix this?
import openpyxl
wb = load_workbook('Test.xlsm')
ws = wb.worksheets[1]
ws.cell(row=1, column=1).value = 999999
wb.save('Test.xlsm')
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook('Test.xlsm',keep_vba=True)
ws = wb['Sheet1']
ws.cell(row=1,column=1).value = 9999999
wb.save('Test.xlsm')
You need to have 'keep_vba' enabled since you are trying to modify an .xlsm(Excel Macro Enabled Workbook),I don't know why but this seems to work.
wb = load_workbook('Test.xlsm',keep_vba=True)
The line wb = load_workbook('Test.xlsm') should be
wb = openpyxl.load_workbook('Test.xlsm') based on what you have provided.
Also, are you looking in sheet 2? The line
wb.worksheets[1] specifies sheet 2 as sheets are zero-indexed.

Combining excel workbook sheet into one using python

I have roughly 30 excel workbooks I need to combine into one. Each workbook has a variable number of sheets but the sheet I need to combine from each workbook is called "Output" and the format of the columns in this sheet is consistent.
I need to import the Output sheet from the first file, then append the remaining files and ignore the header row.
I have tried to do this using glob/pandas to no avail.
You could use openpyxl. Here's a sketch of the code:
from openpyxl import load_workbook
compiled_wb = load_workbook(filename = 'yourfile1.xlsx')
compiled_ws = compiled['Output']
for i in range(1, 30):
wb = load_workbook(filename = 'yourfile{}.xlsx'.format(i))
ws = wb['Output']
compiled_ws.append(ws.rows()[1:]) # ignore row 0
compiled_wb.save('compiled.xlsx')
Method shown by Clinton c. Brownley in Foundations for Analytics with Python:
execute in shell indicating the path to the folder with excel files ( make sure the argument defining all_workbooks is correct) and then followed by the excel output file as follows:
python script.py <the /path/ to/ excel folder/> < your/ final/output.xlsx>
script.py:
import pandas as pd
import sys
import os
import glob
input_path = sys.argv[1]
output_file = sys.argv[2]
all_workbooks = glob.glob(os.path.join(input_file, '*.xlsx'))
all_df = []
for workbook in all_workbooks:
all_worksheets = pd.read_excel(workbook, sheetname='Output', index_col=None)
for worksheet, data in all_worksheets.items:
all_df.append(data)
data_concatenated = pd.concat(all_df, axis=0, ignore_index=True)
writer = pd.ExcelWriter(output_file)
data_concatenated.to_excel(writer, sheetname='concatenated_Output', index=False)
writer.save()
This will probably get down-voted because this isn't a Python answer, but honestly, I wouldn't use Python for this kind of task. I think you are far better off installing the AddIn below, and using that for the job.
https://www.rondebruin.nl/win/addins/rdbmerge.htm
Click 'Merge all files from the folder in the Files location selection' and click 'Use a Worksheet name' = 'Output', and finally, I think you want 'First cell'. Good luck!

Save XLSX file to a specified location using OpenPyXL

I'm having an issue saving my file to a certain location on my Raspberry PI (Raspbian) computer. I'm wanting the XLSX file to be saved directly to my desktop rather than the folder holding the Python Script. When I do wb.save("FileName.xlsx"), It only saves it to the location where the Python Script is located.
Here's my code:
from openpyxl import Workbook
wb = Workbook()
ws1 = wb.active
ws1.title = "1st Hour"
wb.save('FileName.xlsx')
Okay for any user, you can write
from openpyxl import Workbook
import getpass
wb = Workbook()
ws1 = wb.active
ws1.title = "1st Hour"
wb.save('/home/'+getpass.getuser()+'/Desktop/FileName.xlsx')
On windows systems: first you must copy the path, for example this path:
C:\Users\obada yahya\Desktop\python
Now you must add another \ after each already existing \ to the path:
import openpyxl as xl
wb=xl.Workbook()
wb.save("C:\\Users\\obada yahya\\Desktop\\python\\obada12.xlsx")
Now it will work fine.

How to delete an existing worksheet in excel file using xlutils, xlwt, xlrd with python

I tried to search many places but dit not see any example snippet of code about how to delete an existing worksheet in excel file by using xlutils or xlwt with python. Who can help me, please?
I just dealt with this and although this is not generally a good coding choice, you can use the internal Workbook_worksheets to access and set the worksheets for a workbook object.
write_book._Workbook__worksheets = [write_book._Workbook__worksheets[0]]
this would strip everything but the first worksheet associated with a Workbook
I just wanted to confirm that I got this to work using the answer David gave. Here is an example of where I had a spreadsheet (workbook) with 40+ sheets that needed to be split into their own workbooks. I copied the master workbook removed all but the one sheet and saved to a new spreadsheet:
from xlrd import open_workbook
from xlutils import copy
workbook = open_workbook(filepath)
# Process each sheet
for sheet in workbook.sheets():
# Make a copy of the master worksheet
new_workbook = copy.copy(workbook)
# for each time we copy the master workbook, remove all sheets except
# for the curren sheet (as defined by sheet.name)
new_workbook._Workbook__worksheets = [ worksheet for worksheet in new_workbook._Workbook__worksheets if worksheet.name == sheet.name ]
# Save the new_workbook based on sheet.name
new_workbook.save('{}_workbook.xls'.format(sheet.name))
The following method does what you need:
def deleteAllSheetBut(workingFolder, xlsxFILE, sheetNumberNotToDelete=1):
import win32com.client as win32
import os
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = False
excel.DisplayAlerts = False
wb = excel.Workbooks.Open( os.path.join( workingFolder, xlsxFILE ) )
for i in range(1, wb.Worksheets.Count):
if i != sheetNumberNotToDelete:
wb.Worksheets(i).Delete()
wb.Save()
excel.DisplayAlerts = True
excel.Application.Quit()
return
not sure about those modules but u can try win32
from win32com import client
def delete(self, number = 1):
"""
(if the sheet is the first use 1. -1 to use real number)
example: r.delete(1)
"""
sheetnumber = int(number) - 1

Categories

Resources