Try to operate an Excel file using Python (Pandas / XlsxWriter) ①

Try working with Excel files using Pandas or XlsxWriter. I felt like I touched it a little, but I felt that Xlwings was easier to use, It's used by Pandas, so let's use it.

Click here for reading files

reference

pandas XlsxWriter

sample

This time, we will create an Excel file.

# import lib
import pandas as pd
import numpy as np

# create data
dates = pd.date_range("20130101", freq='D', periods=6)
df = pd.DataFrame(np.random.randn(6,4),index = dates, columns = list("ABCD"))

# set data
fileName = "hoge.xlsx"
writer = pd.ExcelWriter(fileName)
df.to_excel(writer, sheet_name="hoge1")
df.T.to_excel(writer, sheet_name="hoge2")

workbook  = writer.book
worksheet1 = writer.sheets['hoge1']
worksheet2 = writer.sheets['hoge2']

# Add some cell formats.
format1 = workbook.add_format()
format1.set_font_color('red')

index = workbook.add_format({'align': 'left'})
index.set_font_color('blue')

# Set the column width and format.
worksheet1.set_column('C:C', None, index)
worksheet2.set_column('B:B', 20, format1)

# save
writer.save()

Recommended Posts

Try to operate an Excel file using Python (Pandas / XlsxWriter) ①
Try to operate an Excel file using Python (Pandas / XlsxWriter) ②
Try to operate Excel using Python (Xlwings)
[Python] How to output a pandas table to an excel file
How to read an Excel file (.xlsx) with Pandas [Python]
How to paste a CSV file into an Excel file using Pandas
[Python] How to read excel file with pandas
Try to operate Facebook with Python
Create an Excel file with Python3
How to convert Python to an exe file
Quickly create an excel file with Python #python
[Python] [Excel] Operate an Excel sheet from Python using openpyxl (using a test sheet as an example)
Try to create an HTTP server using Node.js
Operate Redmine using Python Redmine
Try using Tweepy [Python2.7]
Operate Excel with Python (1)
Operate Excel with Python (2)
Try creating a compressed file using Python and zlib
(Python) Try to develop a web application using Django
Create an image file using PIL (Python Imaging Library).
Try to operate DB with Python and visualize with d3
How to convert JSON file to CSV file with Python Pandas
Type after reading an excel file with pandas read_excel
Try to extract high frequency words using NLTK (python)
Use Pandas to write only the specified lines of the data frame to an excel file
Reading, summarizing, visualizing, and exporting time series data to an Excel file with Python
Every time I try to read a csv file using pandas, I get a numpy error.
[Python] You can save an object to a file by using the pickle module.
I get an error when I try to raise Python to 3 series using pyenv on Catalina
Operate Excel with Python openpyxl
Try to make it using GUI and PyQt in Python
Convert from Pandas DataFrame to System.Data.DataTable using Python for .NET
[Python] Convert list to Pandas [Pandas]
[Python] Try using Tkinter's canvas
Try to understand Python self
Try using Kubernetes Client -Python-
Post to Twitter using Python
Start to Selenium using python
Paste the image into an excel file using Python's openpyxl
An introduction to Python Programming
[Python] How to change EXCEL file saved in xlsb to xlsx
[Python Kivy] How to create an exe file with pyinstaller
Data analysis using python pandas
Export pandas dataframe to excel
Try to decipher the garbled attachment file name with Python
Convert Excel file to text in Python for diff purposes
Read the old Gakushin DC application Word file (.doc) from Python and try to operate it.
Process Splunk execution results using Python and save to a file
Try to extract a character string from an image with Python3
Operate Maya from an external Python interpreter using the rpyc module
Try to analyze online family mahjong using Python (PART 1: Take DATA)
Build and try an OpenCV & Python environment in minutes using Docker
Excel file column addition and row deletion processing using Python Openpyxl
Try to operate the database using Python's ORM Peewee (August 2019 version)
Try to log in to Netflix automatically using python on your PC
Python hand play (RDKit descriptor calculation: SDF to CSV using Pandas)
Get the formula in an excel file as a string in Python
Try adding a wall to your IFC file with IfcOpenShell python
Get RSS feed using Python + pandas → Post to Mattermost & Save to DB
[For Python] Quickly create an upload file to AWS Lambda Layer
How to install python using anaconda