Let's play with Excel with Python [Beginner]

Introduction

"Try to count the number of customers in the city, ward, and county from the address list in Python" https://qiita.com/kkhouse/items/d8ff2c67cef686a508f8

It is a continuation of.

Last time, I wrote a code that calculates the number of customers in any district using the member's address list and makes it a list.

Since I learned a lot, I will post the code explanation of OpenPyXl used.

Commentary

Completion code

As I mentioned in the previous article, here is the completed code. The conditional branch is long ~

main.py


import csv
import sys
import os
import openpyxl as px

#csv → xlsx conversion
wb = openpyxl.Workbook()
ws = wb.active
query_word = sys.argv[1] 

f = open("./data_csv/{}.csv".format(query_word))
reader = csv.reader(f, delimiter=":")
for row in reader:
  ws.append(row)
f.close()

wb.save("./data_xlsx/{0}{1}.xlsx".format(query_word,"_Aggregate"))


#Write to xlsx in Python



folder = "data_xlsx"

filePath = []
for root,dirs,files in os.walk(folder):
  for fname in files:
    fnamePath = os.path.join(root,fname)
    if fnamePath.find(".xlsx")!= -1:
      filePath.append(fnamePath)
    if filePath ==[]:
      print("there is no data!")
xlsx_files_list = [s for s in filePath if query_word in s]



#Get the created xlsx
wb = px.load_workbook(xlsx_files_list[0])
ws = wb[wb.sheetnames[0]]

#List all addresses in xlsx


Miyagi_col =["Ishinomaki City","Miyagino Ward","Wakabayashi Ward","Aoba Ward","Izumi Ward","Taichi Ward","Tagajo","Other"]


adress_list = []

#Empty list of each city
isinomaki =[]
miyagino =[]
wakabayasi =[]
aoba =[]
izumi =[]
taihaku =[]
tagazyo =[]
hoka =[]

#List of each municipality
for i in range(ws.max_row):
  adress_list.append(ws.cell(row=i+1, column=1).value)

  if "Ishinomaki City" in ws.cell(row=i+1,column=1).value:
    isinomaki.append(ws.cell(row=i+1,column=1).value)

  elif "Miyagino Ward" in ws.cell(row=i+1,column=1).value:
    miyagino.append(ws.cell(row=i+1,column=1).value)

  elif "Wakabayashi Ward" in ws.cell(row=i+1,column=1).value:
    wakabayasi.append(ws.cell(row=i+1,column=1).value)

  elif "Aoba Ward" in ws.cell(row=i+1,column=1).value:
    aoba.append(ws.cell(row=i+1,column=1).value)

  elif "Izumi Ward" in ws.cell(row=i+1,column=1).value:
    izumi.append(ws.cell(row=i+1,column=1).value)

  elif "Taichi Ward" in ws.cell(row=i+1,column=1).value:
    taihaku.append(ws.cell(row=i+1,column=1).value)

  elif "Tagajo" in ws.cell(row=i+1,column=1).value:
    tagazyo.append(ws.cell(row=i+1,column=1).value)

  else:
    hoka.append(ws.cell(row=i+1,column=1).value)

pre_list = [isinomaki,miyagino,wakabayasi,aoba,izumi,taihaku,tagazyo,hoka]

#Column of list to create
for i,j in zip(list(range(1,9)),Miyagi_col):
  ws.cell(row=i, column=7).value = j
#List data to create
for i,g in zip(list(range(1,9)),pre_list):
  ws.cell(row=i, column=8).value = len(g)

#Data storage
wb.save("./data_xlsx/{0}{1}.xlsx".format(query_word,"_Aggregate"))

Convert csv file to xlsx

#csv → xlsx conversion
wb = openpyxl.Workbook(
ws = wb.active
query_word = sys.argv[1] 

f = open("./data_csv/{}.csv".format(query_word))
reader = csv.reader(f, delimiter=":")
for row in reader:
  ws.append(row)
f.close()

wb.save("./data_xlsx/{0}{1}.xlsx".format(query_word,"_Aggregate"))

Convert CSV format to XLSX

wb = openpyxl.Workbook () #Create new ws = wb.active #active sheet

query_word = sys.argv[1] f = open("./data_csv/{}.csv".format(query_word)) Get the filename entered in the terminal with sys.argv and the format method (Python main.py ○○ (file name))

reader = csv.reader(f, delimiter=":") for row in reader: ws.append(row) f.close() load csv

wb.save ("./data_xlsx/{0}{1}.xlsx".format (query_word, "_aggregate")) Convert to xlsx file and save.

At the end

It's time so far! Next time from the code that looks for XLSX files in the directory

Recommended Posts

Let's play with Excel with Python [Beginner]
Let's run Excel with Python
Play with 2016-Python
Excel with Python
Handle Excel with python
Operate Excel with Python (1)
Operate Excel with Python (2)
Python hand play (let's get started with AtCoder?)
Operate Excel with Python openpyxl
Let's play with 4D 4th
Let's play with Amedas data-Part 1
Let's play with Amedas data-Part 4
[Python] Play with Discord's Webhook.
Let's write python with cinema4d.
Play RocketChat with API / Python
Let's play with Amedas data-Part 3
Let's play with Amedas data-Part 2
Web scraping beginner with python
Let's build git-cat with Python
[Let's play with Python] Make a household account book
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
solver> Link> Solve Excel Solver with python
Let's make a GUI with python.
Create an Excel file with Python3
Let's do image scraping with Python
[Let's play with Python] Image processing to monochrome and dots
Handle Excel CSV files with Python
[Beginner] Extract character strings with Python
Let's make a graph with python! !!
Let's analyze voice with Python # 1 FFT
Excel aggregation with Python pandas Part 1
[Let's play with Python] Aiming for automatic sentence generation ~ Completion of automatic sentence generation ~
Play video with sound with python !! (tkinter / imageio)
[Introduction to WordCloud] Let's play with scraping ♬
Let's make a shiritori game with Python
Play audio files from Python with interrupts
Draw Nozomi Sasaki in Excel with python
Quickly create an excel file with Python #python
Convert Excel data to JSON with python
[Introduction to Python] Let's use foreach with Python
Let's make a voice slowly with Python
I want to play with aws with python
Excel aggregation with Python pandas Part 2 Variadic
[Easy Python] Reading Excel files with openpyxl
[Python] Let's make matplotlib compatible with Japanese
Create Excel file with Python + similarity matrix
Let's do MySQL data manipulation with Python
Let's make a web framework with Python! (1)
[Easy Python] Reading Excel files with pandas
Excel table creation with Python [Progress management table]
Let's make a Twitter Bot with Python!
Let's develop an investment algorithm with Python 1
Let's get along with Python # 0 (Environment construction)
Let's make a web framework with Python! (2)
[Blender x Python] Let's get started with Blender Python !!
Beginner ABC154 (Python)
Play with Prophet
FizzBuzz with Python3
Scraping with Python
python beginner memo (9.2-10)
Statistics with python