By using excel=False ( default value is True ) we can get string output, this we can't paste in Excel file ( to match the cells ) .
df.to_clipboard(index=False,excel=False)
Data from MySQL table to html file
Read on how to connect to MySQL database and then collected the records of student table by using read_sql() to a DataFrame. Finally we will copy the data to clip board by using to_clipboard().
import mysql.connector
import pandas as pd
my_connect = mysql.connector.connect(
host="localhost",
user="root",
passwd="*****",
database="my_tutorial"
)
####### end of connection ####
sql="SELECT * FROM student limit 0,5"
df = pd.read_sql(sql,my_connect )
df.to_clipboard(index=False)
Using SQLAlchemy
We will collect records from our sample student table in MySQL database and copy the output to Clipboard by using to_clipboard(). Collect SQL dump of sample student table below.
Read more on MySQL with SQLAlchemy connection.
import pandas as pd
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
sql="SELECT * FROM student LIMIT 0,10 "
df = pd.read_sql(sql,my_conn)
df.to_clipboard()
From Excel file to Clipboard as output
In above code we have created one DataFrame by taking data from a MySQL database table. We can create DataFrame by using any excel data or by using any csv file or from any other sources. ( check here to create a DataFrame from 8 different sources )
Once a DataFrame is created, then using that we can copy output to Clipboard by using to_clipboard(). Here is one example to read one Excel file to a DataFrame and generate the output to Clipboard, you can paste the clipboard data to text file or Excel file.
We used read_excel() to read our sample student.xlsx file.
df=pd.read_excel("D:\\my_data\\student.xlsx") # Path of the file.
df.to_clipboard()