SlideShare a Scribd company logo
1 of 22
NAME:SANTHAPURI VINEELA
ROLL.NO:22951A62B6
BRANCH:CSE(CYBER SECURITY)
SUBJECT:PYTHON PROGRAMMING
TOPIC:PYTHON FOR SPREADSHEET USERS
Python for
Spreadsheet
Users
• Excel is one of the popular spreadsheets for
data management. While it is tedious to
write and update the data in a long Excel
sheet, Python helps in minimizing this task
and helps easy creation, reading, writing of
Excel sheet. This can be done by various
Python libraries,3 of which will be discussed
in this article.
Using Openpyxl Library
• penpyxl is a Python library for reading and writing Excel (with
extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows
Python program to read and modify Excel files.
pip install openpyxl
INSTALLATION
Creating or Writing a
Workbook
# import modules
from openpyxl import Workbook
import datetime
wb = Workbook()
ws = wb.active
# assign sheet
ws['A1'] = 75
# add data
ws.append([1, 2, 3])
ws.append([4, 5, 6])
ws.append([8, 10, 12])
ws['A2'] = datetime.datetime.now()
# save spreadsheet
wb.save("test.xlsx")
• We write the code in
our code editor and
save the code
Output:
Explanation:
 We first import the library and the Spreadsheet is given a
workbook instance. Afterward, we assign random values
corresponding to columns A. Here we have appended in A1 and
A2. We can also override the original values as 1 in A1 and 4 in
A2 has been overwritten as respective dates and time. Lastly, we
save the sheet, where it’s saved as “test”.
Reading a Workbook: # import modules
from openpyxl import
load_workbook
from openpyxl import Workbook
# load spreadsheet
wb = load_workbook(filename =
'test.xlsx')
 We can also read a
Workbook from this
program
 This opens the same
file which we had
opened earlier
Inserting Images in the Workbook:
# import modules
from openpyxl import Workbook
from openpyxl import Workbook
from openpyxl.drawing.image import Image
Spreadsheet = Workbook()
worksheet = Spreadsheet.active
wb = Workbook()
ws = wb.active
# assign title
ws['A1'] = 'Two logos demonstrated'
# create an image
img = Image('gfg.png')
img2=Image('gfg2.png')
# add image
ws.add_image(img, 'A1')
ws.add_image(img2, 'H1')
wb.save('logo.xlsx')
• We can also insert a few images
in our Excel sheet as
demonstrated in the code
Output
Explanation:
• We have to download the pillow
library for dependency and import
the sub-libraries as shown. We
now download two images we like
and just give them the designated
row, and we have our image
inserted!
Using XLWT Library
oThis is a library for developers to use to generate
spreadsheet files compatible with Microsoft Excel
versions 95 to 2003. The package itself is pure Python
with no dependencies on modules or packages outside
the standard Python distribution.
oWe can make values bold, colored, italic, etc. Also, the
font size can be controlled.
Installation
Install the python package by the
following command
pip3 install xlwt
Or
pip install xlwt
Creating a Workbook
# import module
import xlwt
from datetime import datetime
# assign attributes
style0 = xlwt.easyxf('font: name Verdana, color-index
green, bold on',)
style1 = xlwt.easyxf(num_format_str='D-MMM-YY')
style2 = xlwt.easyxf(
'font: name Times New Roman, color-index orange,
bold on',)
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
# add data
ws.write(0, 0, 156, style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1, style2)
ws.write(2, 1, 1, style2)
ws.write(2, 2, xlwt.Formula("A3+B3"))
# printing results
wb.save('example.xls')
• Given code which gives
explore the formatting
value,something not
explored in
Output:
Explanation:
 We import the library and then declare 3 different styles to apply to the values. Style 1
explores one family and color green, while the third one is Times New Roman, and the
color is orange with styling as bold. Stye 1 is the format of dates if mentioned, and it’s D-
MM-YY
 We write the values this time in respective rows and columns both starting with 0
indexes. Hence, ws.write(row, column, value, style). Style is optional here.
Using Xlsx
writer
• XlsxWriter is a Python module for writing
files in the Excel 2007+ XLSX file
format.XlsxWriter can be used to write
text, numbers, formulas, and hyperlinks to
multiple worksheets, and it supports
features Here is a simple example:
Conditional formatting., Worksheet
PNG/JPEG/BMP/WMF/EMF images, Rich
multi-format strings, Cell comments.,
Integration with Pandas., Textboxes.,
Support for adding Macros.
Installation
•Install the
below library
from the
commands
pip install
XlsxWriter
Or
pip3 install
XlsxWriter
Example
# import module
import xlsxwriter
# Create an new Excel file and add a
# worksheet.
workbook = xlsxwriter.Workbook('demo.xlsx')
ws = workbook.add_worksheet()
# Widen the first column to make the
# text clearer.
ws.set_column('A:A', 20)
# Adding a bold format to use to highlight
# cells.
style = workbook.add_format({'bold': True})
# add data
ws.write('A1', 'Geeks')
ws.write('A2', 'for Geeks', style)
ws.insert_image('B5', 'logo.png')
workbook.close()
 This is another way to create an
Excel sheet and with a wide range
of options as well as images of all
extensions supported. Let's take a
code example.
Explanation
 We first import the said library and create
a workbook (demo.xlsx) and start by
setting a wide column and giving the size
as 20. We then declare a style name bold
where we set style as true. Next, we write
the word “Geeks” with no style but the
next word “for Geeks” is applied to the
style we have given. Notice as compared
to the earlier example we use true instead
of on and easyxf function to declare a
style. This makes xlsxWriter the most
readable and user-free syntax compared
to the other two.
Output:
PYTHON FOR SPREADSHEET USERS.pptx

More Related Content

Similar to PYTHON FOR SPREADSHEET USERS.pptx

CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
faithxdunce63732
 
XLS PE How To Tutorials Tips & Tricks
XLS PE How To Tutorials Tips & TricksXLS PE How To Tutorials Tips & Tricks
XLS PE How To Tutorials Tips & Tricks
guest92a5de
 
XLS Processor Engine How To, Tutorials, Tips & Tricks
XLS Processor Engine How To, Tutorials, Tips & TricksXLS Processor Engine How To, Tutorials, Tips & Tricks
XLS Processor Engine How To, Tutorials, Tips & Tricks
Earl Grau
 
( 13 ) Office 2007 Coding With Excel And Excel Services
( 13 ) Office 2007   Coding With Excel And Excel Services( 13 ) Office 2007   Coding With Excel And Excel Services
( 13 ) Office 2007 Coding With Excel And Excel Services
LiquidHub
 

Similar to PYTHON FOR SPREADSHEET USERS.pptx (20)

XPages -Beyond the Basics
XPages -Beyond the BasicsXPages -Beyond the Basics
XPages -Beyond the Basics
 
Application of code composer studio in digital signal processing
Application of code composer studio in digital signal processingApplication of code composer studio in digital signal processing
Application of code composer studio in digital signal processing
 
Python-Libraries,Numpy,Pandas,Matplotlib.pptx
Python-Libraries,Numpy,Pandas,Matplotlib.pptxPython-Libraries,Numpy,Pandas,Matplotlib.pptx
Python-Libraries,Numpy,Pandas,Matplotlib.pptx
 
Open erp technical_memento_v0.6.3_a4
Open erp technical_memento_v0.6.3_a4Open erp technical_memento_v0.6.3_a4
Open erp technical_memento_v0.6.3_a4
 
Python Workshop Day - 2 (REVA University)
Python Workshop Day - 2 (REVA University)Python Workshop Day - 2 (REVA University)
Python Workshop Day - 2 (REVA University)
 
[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the Basics[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the Basics
 
ch 2. Python module
ch 2. Python module ch 2. Python module
ch 2. Python module
 
EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4
 
Flex 4.5 jeyasekar
Flex 4.5  jeyasekarFlex 4.5  jeyasekar
Flex 4.5 jeyasekar
 
Session 3.2 Your first excel and word automations
Session 3.2 Your first excel and word automationsSession 3.2 Your first excel and word automations
Session 3.2 Your first excel and word automations
 
3 Ways to Get Started with a React App in 2024.pdf
3 Ways to Get Started with a React App in 2024.pdf3 Ways to Get Started with a React App in 2024.pdf
3 Ways to Get Started with a React App in 2024.pdf
 
Intro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & ArraysIntro To C++ - Class #18: Vectors & Arrays
Intro To C++ - Class #18: Vectors & Arrays
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
 
XLS PE How To Tutorials Tips & Tricks
XLS PE How To Tutorials Tips & TricksXLS PE How To Tutorials Tips & Tricks
XLS PE How To Tutorials Tips & Tricks
 
XLS Processor Engine How To, Tutorials, Tips & Tricks
XLS Processor Engine How To, Tutorials, Tips & TricksXLS Processor Engine How To, Tutorials, Tips & Tricks
XLS Processor Engine How To, Tutorials, Tips & Tricks
 
OpenWhisk by Example - Auto Retweeting Example in Python
OpenWhisk by Example - Auto Retweeting Example in PythonOpenWhisk by Example - Auto Retweeting Example in Python
OpenWhisk by Example - Auto Retweeting Example in Python
 
Vsto 3 Snug
Vsto 3 SnugVsto 3 Snug
Vsto 3 Snug
 
Vsto 3 Excel Add-in SNUG
Vsto 3 Excel Add-in SNUGVsto 3 Excel Add-in SNUG
Vsto 3 Excel Add-in SNUG
 
How to set up es lint for react projects
How to set up es lint for react projectsHow to set up es lint for react projects
How to set up es lint for react projects
 
( 13 ) Office 2007 Coding With Excel And Excel Services
( 13 ) Office 2007   Coding With Excel And Excel Services( 13 ) Office 2007   Coding With Excel And Excel Services
( 13 ) Office 2007 Coding With Excel And Excel Services
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 

PYTHON FOR SPREADSHEET USERS.pptx

  • 2. Python for Spreadsheet Users • Excel is one of the popular spreadsheets for data management. While it is tedious to write and update the data in a long Excel sheet, Python helps in minimizing this task and helps easy creation, reading, writing of Excel sheet. This can be done by various Python libraries,3 of which will be discussed in this article.
  • 3. Using Openpyxl Library • penpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files.
  • 5. Creating or Writing a Workbook # import modules from openpyxl import Workbook import datetime wb = Workbook() ws = wb.active # assign sheet ws['A1'] = 75 # add data ws.append([1, 2, 3]) ws.append([4, 5, 6]) ws.append([8, 10, 12]) ws['A2'] = datetime.datetime.now() # save spreadsheet wb.save("test.xlsx") • We write the code in our code editor and save the code
  • 7. Explanation:  We first import the library and the Spreadsheet is given a workbook instance. Afterward, we assign random values corresponding to columns A. Here we have appended in A1 and A2. We can also override the original values as 1 in A1 and 4 in A2 has been overwritten as respective dates and time. Lastly, we save the sheet, where it’s saved as “test”.
  • 8. Reading a Workbook: # import modules from openpyxl import load_workbook from openpyxl import Workbook # load spreadsheet wb = load_workbook(filename = 'test.xlsx')  We can also read a Workbook from this program  This opens the same file which we had opened earlier
  • 9. Inserting Images in the Workbook: # import modules from openpyxl import Workbook from openpyxl import Workbook from openpyxl.drawing.image import Image Spreadsheet = Workbook() worksheet = Spreadsheet.active wb = Workbook() ws = wb.active # assign title ws['A1'] = 'Two logos demonstrated' # create an image img = Image('gfg.png') img2=Image('gfg2.png') # add image ws.add_image(img, 'A1') ws.add_image(img2, 'H1') wb.save('logo.xlsx') • We can also insert a few images in our Excel sheet as demonstrated in the code
  • 11. Explanation: • We have to download the pillow library for dependency and import the sub-libraries as shown. We now download two images we like and just give them the designated row, and we have our image inserted!
  • 12. Using XLWT Library oThis is a library for developers to use to generate spreadsheet files compatible with Microsoft Excel versions 95 to 2003. The package itself is pure Python with no dependencies on modules or packages outside the standard Python distribution. oWe can make values bold, colored, italic, etc. Also, the font size can be controlled.
  • 13. Installation Install the python package by the following command pip3 install xlwt Or pip install xlwt
  • 14. Creating a Workbook # import module import xlwt from datetime import datetime # assign attributes style0 = xlwt.easyxf('font: name Verdana, color-index green, bold on',) style1 = xlwt.easyxf(num_format_str='D-MMM-YY') style2 = xlwt.easyxf( 'font: name Times New Roman, color-index orange, bold on',) wb = xlwt.Workbook() ws = wb.add_sheet('A Test Sheet') # add data ws.write(0, 0, 156, style0) ws.write(1, 0, datetime.now(), style1) ws.write(2, 0, 1, style2) ws.write(2, 1, 1, style2) ws.write(2, 2, xlwt.Formula("A3+B3")) # printing results wb.save('example.xls') • Given code which gives explore the formatting value,something not explored in
  • 16. Explanation:  We import the library and then declare 3 different styles to apply to the values. Style 1 explores one family and color green, while the third one is Times New Roman, and the color is orange with styling as bold. Stye 1 is the format of dates if mentioned, and it’s D- MM-YY  We write the values this time in respective rows and columns both starting with 0 indexes. Hence, ws.write(row, column, value, style). Style is optional here.
  • 17. Using Xlsx writer • XlsxWriter is a Python module for writing files in the Excel 2007+ XLSX file format.XlsxWriter can be used to write text, numbers, formulas, and hyperlinks to multiple worksheets, and it supports features Here is a simple example: Conditional formatting., Worksheet PNG/JPEG/BMP/WMF/EMF images, Rich multi-format strings, Cell comments., Integration with Pandas., Textboxes., Support for adding Macros.
  • 18. Installation •Install the below library from the commands pip install XlsxWriter Or pip3 install XlsxWriter
  • 19. Example # import module import xlsxwriter # Create an new Excel file and add a # worksheet. workbook = xlsxwriter.Workbook('demo.xlsx') ws = workbook.add_worksheet() # Widen the first column to make the # text clearer. ws.set_column('A:A', 20) # Adding a bold format to use to highlight # cells. style = workbook.add_format({'bold': True}) # add data ws.write('A1', 'Geeks') ws.write('A2', 'for Geeks', style) ws.insert_image('B5', 'logo.png') workbook.close()  This is another way to create an Excel sheet and with a wide range of options as well as images of all extensions supported. Let's take a code example.
  • 20. Explanation  We first import the said library and create a workbook (demo.xlsx) and start by setting a wide column and giving the size as 20. We then declare a style name bold where we set style as true. Next, we write the word “Geeks” with no style but the next word “for Geeks” is applied to the style we have given. Notice as compared to the earlier example we use true instead of on and easyxf function to declare a style. This makes xlsxWriter the most readable and user-free syntax compared to the other two.