web coder tried excel in Python

Introduction

When I wanted to move excel data to an html file, I couldn't reach the itchy part of the tool I searched for, so I searched for something that I could easily bring in the value of excel and write it to html or pug. By the way, the python library openpyxl seemed to be good, so I tried it.

environment

mac Python 3.8.2

Installation

Since there are many installations of python in qiita, I will omit it.

Install the python library openpyxl.

$ pip3 install openpyxl

File preparation

Prepare sample.xlsx that you want to retrieve the value in the same directory and sample.py that you will describe. The contents of the excel file are as follows as a sample. sample_xlsx.png

py file

sample.py


#Import openpyxl
import openpyxl

#Specify the xlsx file to read(sample.xlsx)
xlsx_file = openpyxl.load_workbook("sample.xlsx")

#Specify the sheet of xlsx file to read
xlsx_sheet = xlsx_file["Sheet1"]

#Have an empty list ready
customer_list = []

#Read except the first line
for row in xlsx_sheet.iter_rows(min_row=2):

#Add the retrieved value to the list(They are getting in order from the first column)
    customer_list.append([row[0].value, row[1].value])

#Create and write a pug file
with open("sample.pug", "w") as f:
    for i in customer_list:
        f.write("p" + " " + str(i[0]) + "\n" + "\t" + "span" + " " + str(i[1]) + "\n")

result

sample.pug


p Pochi
span 1 year old
p Taro
span 5 years old
p cheese
span 3 years old

I was able to export to a pug file more easily than I had imagined. After that, I think that you can avoid repeating the same work for a while by adjusting it to a table or something when exporting. Since I touched python for the first time, I would be grateful if you could let me know if there is something wrong with how to write it!

Recommended Posts

web coder tried excel in Python
Python programming in Excel
Web scraping notes in python3
python web scraping-get elements in bulk
I tried web scraping with python.
Hit the web API in Python
I tried Line notification in Python
I tried to implement PLSA in Python
I tried to implement permutation in Python
Draw Nozomi Sasaki in Excel with python
I tried to implement PLSA in Python 2
I tried using Bayesian Optimization in Python
I tried to implement ADALINE in Python
I tried to implement PPO in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Disassemble in Python
Reflection in Python
Constant in python
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Excel with Python
Flatten in python
flatten in python
I tried web scraping using python and selenium
I tried playing a typing game in Python
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
Automatically create word and excel reports in python
[Python] Implemented automation in excel file copying work
Steps to develop a web application in Python