Stylish technique for pasting CSV data into Excel with Python

Thing you want to do

Get CSV data with Python, format, paste Excel I don't think there is such a situation, but unfortunately I was there.


2016/02/14 postscript @Okadate left an ultra-stylish technique in the comments. Please refer to that. After that, it will be the wreckage that was created by the tension at midnight.


environment

Windows7 Python3.4

I want to paste this kind of CSV data like this

sample.csv


 ,Label,Area,Mean,Min,Max
1,4mas.tif,40000,0.000,0,0
2,10mas.tif,40000,32757.335,30635,34880
3,25mas.tif,40000,32784.505,1039,64761
4,40mas.tif,40000,32836.377,1371,65157

If you copy the above CSV and paste the value into Excel jg.PNG It will be like this.

The ideal is gjapo.PNG

I like this

sample.py


import win32com.client as win32
xlApp = win32.Dispatch("Excel.Application")
xlApp.Visible = 1
wb = xlApp.Workbooks.Open("C:/Users/user/Documents/1.xls")
Sheet = xlApp.Workbooks(1).Sheets(1)
f=open("C:/Users/user/Documents/sample.csv",'r')
for i,line in enumerate(f.readlines()):#This is miso
    for n,label in enumerate(line.split(",")):
        cell = Sheet.Cells(i+1,n+1)#In win32, to start Cell from 1
        cell.Value = label

That is the miso. Since enumerate loops with the index, add +1 to it to match the Cell. It's a Python-like way of writing (I just want to say)

It would be a moment if I did it by hand, but I wanted to do it in Python. (I came up with it in the middle of the night, so I feel like I overlooked something important)

Please let me know if there is another good way

problem

problem. .. .. hgap.PNG It will be something like "wrap around and display the whole" without permission. It's not good.

Recommended Posts

Stylish technique for pasting CSV data into Excel with Python
Handle Excel CSV files with Python
[For beginners] Read Excel / CSV files into DataFrame with Google Colaboratory
INSERT into MySQL with Python [For beginners]
Convert Excel data to JSON with python
[Python] Script useful for Excel / csv processing
Excel with Python
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Write CSV data to AWS-S3 with AWS-Lambda + Python
Make JSON into CSV with Python from Splunk
Handle Excel with python
Csv tinkering with python
Data analysis with Python
Operate Excel with Python (1)
Operate Excel with Python (2)
Data analysis for improving POG 1 ~ Web scraping with Python ~
Process csv data with python (count processing using pandas)
Sample data created with python
Operate Excel with Python openpyxl
Read csv with python pandas
Graph Excel data with matplotlib (1)
[Python] GUI for inserting TeX format into PowerPoint with 2 clicks [PowerPoint]
Python for Data Analysis Chapter 4
Let's run Excel with Python
Notes on importing data from MySQL or CSV with Python
Get Youtube data with python
Graph Excel data with matplotlib (2)
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
Python code for writing CSV data to DSX object storage
Write to csv with Python
Python for Data Analysis Chapter 2
Get data from analytics API with Google API Client for python
Download csv file with python
Python for Data Analysis Chapter 3
Read json data with python
Make multiple numerical elevation data into a single picture with Python
How to import CSV and TSV files into SQLite with Python
[Pandas] I tried to analyze sales data with Python [For beginners]
[For beginners] Script within 10 lines (3. Data acquisition / csv conversion with datareader)
[Python] Read a csv file with a large data size using a generator
Create a USB boot Ubuntu with a Python environment for data analysis
Create and return a CP932 CSV file for Excel with Chalice
solver> Link> Solve Excel Solver with python
Make apache log csv with python
[Python] Write to csv file with Python
Output to csv file with Python
[Python] Get economic data with DataReader
Create an Excel file with Python3
Let's play with Excel with Python [Beginner]
Python data structures learned with chemoinformatics
Notes for me python csv graph
Preprocessing template for data analysis (Python)
Data formatting for Python / color plots
Easy data visualization with Python seaborn.
Reading and writing CSV with Python
Excel, csv import, export with Django
Process Pubmed .xml data with python
Data analysis starting with python (data visualization 1)
Getting Started with Python for PHPer-Functions
Data analysis starting with python (data visualization 2)
Python application: Data cleansing # 2: Data cleansing with DataFrame