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.
mac Python 3.8.2
Since there are many installations of python in qiita, I will omit it.
Install the python library openpyxl.
$ pip3 install openpyxl
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.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")
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