程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python uses pandas and xlwt to write and read excel in XLS format

編輯:Python

1、 Third party libraries used :
        pandas  、xlwt

2、 Get data , This time, it is entered manually

       + '\t' Purpose : Prevent input numbers from being too long , When writing a table , Written as scientific counting

# agent
jbr = input(' Please enter the name of the handler :')
# The ID number of the handler
jbrsfzh = input(' Please enter the ID number of the handler :'+'\t')
# Phone number of the handler
jbrdh = int(input(' Please enter the phone number of the handler :'))

3、 Create a workbook Set encoding

workbook=xlwt.Workbook(encoding='utf-8')

4、 Create a worksheet

        sheet The name of is the page name of the table

worksheet=workbook.add_sheet('sheet1')

5、 Set the style of the table ( Including line height , Column width , Font style 、 Color, etc )

# Initialize style
style=xlwt.XFStyle()
# Create fonts for styles
font=xlwt.Font()
font.name='new'
font.height=20*16 # font size ,16 For the font size ,20 Is the unit of measurement
style.font=font
# Write column width
worksheet.col(0).width = 3333
worksheet.col(1).width = 8888
worksheet.col(2).width = 6666

6、 write in excel,

 Parameters of the corresponding That's ok , Column , value 
worksheet.write(1,0,jbr)
worksheet.write(1,1,jbrsfzh)
worksheet.write(1,2,jbrdh)

7、 Save to table

workbook.save('set.xls')

8、 Reading data

df=pd.read_excel('D:/gaodengtest/test_python/set.xls')

9、 Go get a line 、 The value of a column

iloc The next two values represent rows 、 Column index , You can also write only a single , Represents all data of a row or column

# first line 、 First column
jbr = df.iloc[0][0]
# The first 4 That's ok 、 The first 5 Column
jbrsfzh = int(df.iloc[3][4])
# The first 3 That's ok
jbrdh = int(df.iloc[2])


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved