發佈日期:
分類:
測試Microsoft Excel如何執行Python
01. 既然現時Excel 中可以支援Python功能,就直接在Excel中,測試一Python的程式碼。
02. Get cells A1 content and assign to x。
x = xl("A1")
03. Get first character of cells A1。
xl("A1")[0:1]
04. Get first 2 characters of cells A1。
xl("A1")[0:2]
05. Get all characters of cells A1 except the last character。
xl("A1")[0:-1]
06. Get all characters of cells A1 except the last 2 characters。
xl("A1")[0:-2]
07. Get last character of cells A1。
xl("A1")[-1]
08. Get sum of cells A1 to A8。
import pandas as pd df = pd.DataFrame(xl("A1:A8")) s = df.sum() s.iloc[0]
09. Plot bar chart for cells A1 to A8 in x-axis and B1 to B8 in y axis。
import pandas as pd import matplotlib.pyplot as plt df1 = pd.DataFrame(xl("A1:A8")) df2 = pd.DataFrame(xl("B1:B8")) df.insert(1, 'column-a', df1) df.insert(1, 'column-b', df2) df.plot.bar(x='column-a', y='column-b', title='Bar Plot') plt.show()
10. Print value from each row from A1 to A8 by for-loop。
import pandas as pd df = pd.DataFrame(xl("A1:A8")) print("Iterating through rows:") for index, row in df.iterrows(): print(f"Row {index}:") print(row)
11. Print value from each column from A1 to C1 by for-loop。
import pandas as pd df = pd.DataFrame(xl("A1:C1")) print("Iterating through columns:") for column, value in df.iloc[0].items(): print(f"{column}: {value}")
12. Example for if-else, for end if statement, the end of if block is determined by indentation of position of if statement。
if xl("A1") == "A": print ("High") else: print ("Low") print ("000")
發佈留言