1. 程式人生 > 實用技巧 >Python讀取excel

Python讀取excel

openpyxl

# Write a workbook
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
wb = Workbook()
dest_filename = 'empty_book.xlsx'
ws1 = wb.active
ws1.title = "range names"
for row in range(1, 40):
        ws1.append(range(600))
ws2 = wb.create_sheet(title="Pi")
ws2['F5'] = 3.14
ws3 = wb.create_sheet(title="Data")
for row in range(10, 20):
     for col in range(27, 54):
         _ = ws3.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))
wb.save(filename = dest_filename)

# Read an existing workbook
from openpyxl import load_workbook
wb = load_workbook(filename = 'empty_book.xlsx')
sheet1 = wb['Sheet1']
print(sheet_1['D1'].value)

pandas

import pandas as pd
titanic = pd.read_csv("data/titanic.csv")
titanic = pd.read_excel('titanic.xlsx', sheet_name='passengers')
ages = titanic["Age"]
ages.head()
age_sex = titanic[["Age", "Sex"]]
age_sex.head()

rpa

excel=rpa.excel.open(path,visible=True,readonly=True)
sheet=excel.get_sheet('質檢表')
rowsOfheet=sheet.row_count()