Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 89a3ded

Browse files
committed
Add Python Excel code
1 parent b3ccfed commit 89a3ded

9 files changed

+12038
-0
lines changed

excel/awesomeworkbook.xlsx

5.33 KB
Binary file not shown.

excel/regiondata.xlsx

8.59 KB
Binary file not shown.

excel/some_data.txt

Lines changed: 11767 additions & 0 deletions
Large diffs are not rendered by default.

excel/some_names.csv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Sam,Smith,3 Jackson Drive,Kentwood,MI,49508,45000
2+
Sally,Holmes,12 Front Drive,Boise,ID,83704,30000
3+
Noah,Johnson,81 Emerson Way,C Springs,CO,80922,18000
4+
Emma,Williams,17 Upside Lane,Watauga,TX,76148,120000
5+
Zach,Price,99 Eleven Way,Worthington,OH,43085,68000
6+
Olivia,Jones,712 Front Street,C Valley,CA,94546,90000
7+
Sam,Miller,75 High Street,Kentwood,MI,49508,45000
8+
Ava,Brown,24 Seven Street,Boise,ID,83704,30000
9+
James,Miller,11 Breaker Lane,Worthington,OH,43085,68000

excel/some_names_modified.xlsx

5.19 KB
Binary file not shown.

excel/stock_options.xlsx

93.5 KB
Binary file not shown.

excel/stylish.xlsx

76.9 KB
Binary file not shown.

extracted_data.xlsx

4.82 KB
Binary file not shown.

pythonexcel.py

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
# Excel Data Frame
2+
import pandas as pd
3+
from openpyxl.workbook import Workbook
4+
5+
dataframe_excel = pd.read_excel('excel/regiondata.xlsx')
6+
7+
print(dataframe_excel)
8+
9+
# CSV Data Frame
10+
dataframe_csv = pd.read_csv('excel/some_names.csv')
11+
12+
print(dataframe_csv)
13+
14+
# Setting Column Names
15+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
16+
17+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
18+
print(dataframe_csv)
19+
20+
# Pandas CSV to Excel
21+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
22+
23+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
24+
print(dataframe_csv)
25+
26+
dataframe_csv.to_excel('excel/some_names_modified.xlsx')
27+
28+
# Text File Data Frame
29+
dataframe_txt = pd.read_csv('excel/some_data.txt', delimiter='\t')
30+
31+
print(dataframe_txt)
32+
33+
# Work With Columns In Pandas
34+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
35+
36+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
37+
print(dataframe_csv.columns)
38+
39+
40+
# take the First Name index and this prints out all the values in the First Name column
41+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
42+
43+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
44+
print(dataframe_csv['First'])
45+
46+
# access multiple column’s data
47+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
48+
49+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
50+
print(dataframe_csv[['Address', 'State']])
51+
52+
# look at the Zip column, but only the first two results
53+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
54+
55+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
56+
print(dataframe_csv['Zip'][0:2])
57+
58+
# Work With Rows In Pandas
59+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
60+
61+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
62+
print(dataframe_csv.iloc[2])
63+
64+
# Locate exact cell
65+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
66+
67+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
68+
print(dataframe_csv.iloc[3, 2])
69+
70+
# Saving Extracted Data
71+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
72+
73+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
74+
75+
extracted_data = dataframe_csv[['First', 'Last', 'City']]
76+
77+
stored = extracted_data.to_excel('extracted_data.xlsx', index=None)
78+
79+
# Filter And Sort Data Using Pandas
80+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
81+
82+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
83+
84+
print(dataframe_csv[dataframe_csv['City'] == 'Worthington'])
85+
86+
# checks for all rows where the City is Kentwood *and* the First column has a value of Sam.
87+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
88+
89+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
90+
91+
print(dataframe_csv[(dataframe_csv['City'] == 'Kentwood') & (dataframe_csv['First'] == 'Sam')])
92+
93+
# drop columns using the .drop() function.
94+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
95+
96+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
97+
98+
drop = ['Address', 'Population']
99+
dataframe_csv.drop(columns=drop, inplace=True)
100+
101+
print(dataframe_csv)
102+
103+
# check to see if the State column has a value of OH, and if it does, go ahead and set the new column we defined to True.
104+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
105+
106+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
107+
108+
drop = ['Address', 'Population']
109+
dataframe_csv.drop(columns=drop, inplace=True)
110+
111+
dataframe_csv['T or F'] = False
112+
dataframe_csv.loc[dataframe_csv['State'] == 'OH', 'T or F'] = True
113+
114+
print(dataframe_csv)
115+
116+
# use the .sort_values() method to sort the data on a particular column
117+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
118+
119+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
120+
121+
print(dataframe_csv.sort_values('First'))
122+
123+
# To sort the data in the other direction, just add ascending=False as the second argument
124+
dataframe_csv = pd.read_csv('excel/some_names.csv', header=None)
125+
126+
dataframe_csv.columns = ['First', 'Last', 'Address', 'City', 'State', 'Zip', 'Population']
127+
128+
print(dataframe_csv.sort_values('First', ascending=False))
129+
130+
# Controlling Excel Directly With Openpyxl
131+
import openpyxl
132+
133+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
134+
135+
print(type(workbook))
136+
137+
# How To Access Worksheets
138+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
139+
sheet = workbook['Sheet1']
140+
141+
print(type(sheet))
142+
143+
# check what names exist with a simple call to .sheetnames.
144+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
145+
sheetnames = workbook.sheetnames
146+
147+
print(sheetnames)
148+
149+
# Access Cells In Sheets
150+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
151+
sheet = workbook['Sheet1']
152+
cell = sheet['A3']
153+
154+
print(cell.value)
155+
156+
# access a cell using the .cell() method and passing both the row and column as integers
157+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
158+
sheet = workbook['Sheet1']
159+
cell = sheet.cell(row=4, column=14)
160+
161+
print(cell.value)
162+
163+
# iterate over values in the sheet
164+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
165+
sheet = workbook['Sheet1']
166+
167+
for i in range(2, 7):
168+
cell = sheet.cell(row=i, column=1)
169+
print(cell.value)
170+
171+
# use slicing to select a range of cells
172+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
173+
sheet = workbook['Sheet1']
174+
175+
cell_range = sheet['A1':'A3']
176+
177+
print(cell_range)
178+
179+
# print out the number of items in a column
180+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
181+
sheet = workbook['Sheet1']
182+
183+
column_a = sheet['A']
184+
185+
print(len(column_a))
186+
187+
# show all of the cells that have values in row 1
188+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
189+
sheet = workbook['Sheet1']
190+
191+
row_0 = sheet[1]
192+
193+
print(row_0)
194+
195+
# iterating through columns or rows
196+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
197+
sheet = workbook['Sheet1']
198+
199+
for row in sheet.iter_rows(min_row=1, max_col=3, max_row=2):
200+
for cell in row:
201+
print(cell)
202+
203+
# Creating New Workbooks and Worksheets
204+
workbook = openpyxl.Workbook()
205+
worksheet = workbook.active
206+
207+
worksheet2 = workbook.create_sheet('First Sheet')
208+
worksheet3 = workbook.create_sheet('Second Sheet')
209+
210+
worksheet.title = 'My Awesome Sheet'
211+
212+
print(workbook.sheetnames)
213+
214+
# add some data to one of the Worksheets
215+
workbook = openpyxl.Workbook()
216+
worksheet = workbook.active
217+
218+
worksheet2 = workbook.create_sheet('First Sheet')
219+
worksheet3 = workbook.create_sheet('Second Sheet')
220+
221+
worksheet.title = 'My Awesome Sheet'
222+
worksheet['A1'] = 'Hello Openpyxl'
223+
workbook.save('excel/awesomeworkbook.xlsx')
224+
225+
# How To Format Workbooks
226+
from openpyxl.styles import Font, Alignment, GradientFill
227+
228+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
229+
sheet = workbook['Sheet1']
230+
sheet.insert_rows(1, 2)
231+
sheet.merge_cells('A1:O2')
232+
cell = sheet['A1']
233+
cell.font = Font(color='007742', size=20, italic=True)
234+
cell.value = 'Super Cool And Stylish Spreadsheet'
235+
cell.alignment = Alignment(horizontal='right', vertical='center')
236+
cell.fill = GradientFill(stop=('000000', 'ffffff'))
237+
workbook.save('excel/stylish.xlsx')
238+
239+
# Named Styles In Openpyxl
240+
from openpyxl.styles import Font, Alignment, GradientFill, NamedStyle, Side, Border, PatternFill
241+
242+
workbook = openpyxl.load_workbook('excel/stock_options.xlsx')
243+
sheet = workbook['Sheet1']
244+
sheet.insert_rows(1, 2)
245+
sheet.merge_cells('A1:O2')
246+
cell = sheet['A1']
247+
cell.font = Font(color='007742', size=20, italic=True)
248+
cell.value = 'Super Cool And Stylish Spreadsheet'
249+
cell.alignment = Alignment(horizontal='right', vertical='center')
250+
cell.fill = GradientFill(stop=('000000', 'ffffff'))
251+
252+
highlight = NamedStyle(name='highlight')
253+
highlight.font = Font(bold=True)
254+
bd = Side(style='thick', color='000000')
255+
highlight.border = Border(left=None, top=bd, right=None, bottom=bd)
256+
highlight.fill = PatternFill('solid', fgColor='fde295')
257+
258+
for cell in sheet['3:3']:
259+
cell.style = highlight
260+
261+
workbook.save('excel/stylish.xlsx')
262+

0 commit comments

Comments
 (0)