I couldn't find an entry that suits my purpose
Create an Excel file with Python3. Set a value in the cell. Decorate a little. Make it in the form of a file
MacOS Python3 series
Use a library called openpyxl
.
pip install openpyxl
The following is a commentary on the source.
It is a program to make a multiplication table. The worksheet is a 0-based index.
On the other hand, the cell is an index starting from 1.
The A1 cell on the upper left is
ws["A1"]
And get it as an array of worksheets?
ws.cell(1, 1)
There are two ways to specify it as the first from the top of the cell and the first from the left.
excel.py
import openpyxl
from openpyxl.styles import Font
#Preparation of workbook
wb = openpyxl.Workbook()
#Preparation of worksheet
ws = wb.worksheets[0]
ROWS = COLS = 9
c1 = ws["A1"]
c1.value = "{} x {}".format(ROWS, COLS)
c1.font = Font(bold=True, italic=True)
for y in range(1, ROWS + 1):
for x in range(1, COLS + 1):
if y == 1:
col = ws.cell(1, x + 1)
col.value = x
col.font = Font(bold=True)
if x == 1:
col = ws.cell(y + 1, 1)
col.value = y
col.font = Font(bold=True)
col = ws.cell(y + 1, x + 1)
col.value = x * y
# save
wb.save("Sample.xlsx")
Recommended Posts