Create an Excel file with Python3

Motivation

I couldn't find an entry that suits my purpose

Overview

Create an Excel file with Python3. Set a value in the cell. Decorate a little. Make it in the form of a file

environment

MacOS Python3 series

Preparation

Use a library called openpyxl.

pip install openpyxl

Commentary

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.

Finished image

image.png

Source

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

Create an Excel file with Python3
Quickly create an excel file with Python #python
Create Excel file with Python + similarity matrix
Excel with Python
[Python Kivy] How to create an exe file with pyinstaller
How to read an Excel file (.xlsx) with Pandas [Python]
[Python] Quickly create an API with Flask
Create an add-in-enabled Excel instance with xlwings
Create an English word app with python
Create an upgradeable msi file with cx_Freeze
Handle Excel with python
Operate Excel with Python (1)
Operate Excel with Python (2)
Create an app that guesses students with python
[Python] How to read excel file with pandas
Create an image with characters in python (Japanese)
Create a Photoshop format file (.psd) with python
Send an email with Excel attached in Python
Create an API server quickly with Python + Falcon
Operate Excel with Python openpyxl
Create an environment with virtualenv
Create an API with Django
Let's run Excel with Python
Create 3d gif with python3
Draw netCDF file with python
Creating an egg with python
Create a directory with python
Download csv file with python
Create xlsx file with XlsxWriter
Create an animated GIF local server with Python + Flask
Create an image file using PIL (Python Imaging Library).
Type after reading an excel file with pandas read_excel
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a 2d CAD file ".dxf" with python [ezdxf]
Cut out an image with python
solver> Link> Solve Excel Solver with python
Create plot animation with Python + Matplotlib
Extract the xz file with python
[Python] Create a file & folder path specification screen with tkinter
[Python] Write to csv file with Python
Reading, summarizing, visualizing, and exporting time series data to an Excel file with Python
[Automation with python! ] Part 1: Setting file
Implemented file download with Python + Bottle
Create folders from '01' to '12' with python
Try to operate an Excel file using Python (Pandas / XlsxWriter) ①
Output to csv file with Python
Try to operate an Excel file using Python (Pandas / XlsxWriter) ②
Create an exe file that works in a Windows environment without Python with PyInstaller
Create an LCD (16x2) game with Raspberry Pi and Python
Create a virtual environment with Python!
Create a binary file in Python
I sent an SMS with Python
Let's play with Excel with Python [Beginner]
Handle Excel CSV files with Python
Create an age group with pandas
[Python] How to output a pandas table to an excel file
[Automation with python! ] Part 2: File operation
Draw an illustration with Python + OpenCV
[Python] Send an email with outlook
Create a file uploader with Django
Excel aggregation with Python pandas Part 1