datanitro:python程序员可以对 excel VBA 说不
来源:互联网 发布:缤纷de饰界淘宝 编辑:程序博客网 时间:2024/06/10 11:36
Contents
Installation
Excel Interface
Hello World
Calculating Pi
Sample Scripts
Installation
Download and run DataNitroSetup.exe. You'll be prompted to install several programs.
If you don't have .NET Framework 4, you should install it first. (Our installer will give you an error if it's missing, but most people have it).
To update from an older version, first uninstall it from the control panel, and then run the new installer.
Excel Interface
When you start Excel, you'll have a new DataNitro tab.
The tab has 8 buttons:
- Editor
- Launches a custom Idle editor. You can use it, or any other editor, to edit Python code. Once you're done, save it as a .py file and run it.
- Python Shell
- Launches a python shell you can use to interact with the spreadsheet directly.
- import
- Adds a script to the imported script list.
- remove
- Removes the script from the list.
- Run
- Runs the selected script.
- Stop
- Stops a running script.
- Docs
- This links to the documentation.
- Live Help
- This links to our contact information. You can talk with us on any page by clicking "Chat with us!"
Hello World
Hello world with DataNitro is short:
Cell("A1").value = "Hello, World!"That's it!
Calculating Pi
Let's approximate pi with a Monte Carlo simulation. We'll generate a random 2D point with x and y coordinates between 0 and 1, and see how far it is from the origin; specifically, if it's inside the unit circle. The total area of the points we're choosing from is 1, and the area of the points inside the circle and in our box is pi/4, the fraction of points that fall within our radius should be about pi/4.
We'll use Excel to keep track of our trials, and the variables within each trial. First, let's import random, and label a few cells so we know what's what:
from random import randomCell("A2").value, Cell("A3").value, Cell("A4").value = "Trial:","Hits:","pi is about:"Cell("D1").value, Cell("E1").value, Cell("F1").value = "x","y","x^2 + y^2"Next, we should decide on the number of trials. Let's allow the user to set a number by entering it in the first cell; otherwise, we'll do 100.
trials = Cell("A1").valueif (type(trials)!= int) or (trials < 1): trials = 100Now, we'll run the trials. We'll make a function for this, and keep track of the coordinates of our random points, as well as their distance from the origin, under the headings we made. We'll also keep track of the number of trials.
def pi_calc(n): while Cell("B2").value < n: x, y = Cell("D2"), Cell("E2") r = Cell("F2") x.value, y.value = random(), random() r.value = x.value**2 + y.value**2We need to increment the number of trials each time, and also increment "Hits" if our point falls within the circle.
Cell("B2").value += 1 if r.value <= 1: Cell("B3").value += 1We can now approximate pi as 4 times the fraction of hits to trials:
Cell("B4").value = 4*float(Cell("B3").value)/Cell("B2").valueLet's add a small delay at the end of each run so we can watch the function update.
from time import sleep...def pi_calc(n): ... sleep(0.01)Finally, let's run our function, making sure to reset the number of trials and number of hits beforehand:
Cell("B2").value, Cell("B3").value = 0,0pi_calc(trials)
We're done! Our finished script:
# Generates approximations of pi. Put the number of trials you want in A1. More# trials will result in higher accuracy.from random import randomfrom time import sleepdef pi_calc(n): while Cell("B2").value < n: x, y = Cell("D2"), Cell("E2") r = Cell("F2") x.value, y.value = random(), random() r.value = x.value**2 + y.value**2 Cell("B2").value += 1 if r.value <= 1: Cell("B3").value += 1 Cell("B4").value = 4*float(Cell("B3").value)/Cell("B2").value sleep(0.01)trials = Cell("A1").valueif (type(trials)!= int) or (trials < 1): trials = 100# sheet setupCell("A2").value, Cell("A3").value, Cell("A4").value = "Trial:","Hits:","pi is about:"Cell("D1").value, Cell("E1").value, Cell("F1").value = "x","y","x^2 + y^2"Cell("B2").value, Cell("B3").value = 0,0pi_calc(trials)More trials will give greater accuracy - 1000 should be enough to approximate pi to two digits.
Sample Scripts
DataNitro comes with two sample scripts in the example folder: hello_world.py, a hello world program, and pascal_triangle.py, which prints out Pascal's triangle. You can also download and play withpi.py, a spreadsheet and script for binomial option pricing, and theystockquote script for pulling data from yahoo finance, originally fromhttp://goldb.org/ystockquote.html.
If there are any other examples you'd like to see, or if you come up with one you like, let us know!
- datanitro:python程序员可以对 excel VBA 说不
- DataNitro: 用Python高效处理Excel数据
- 储户真的可以对银行收费说不?
- 可怜的程序员-对EF说不!
- VBA对EXCEL的操作
- 谁说青岛程序员不活跃,青岛程序员俱乐部可以有
- EXCEL VBA运行不显示系统提示
- 雅虎对微软说"不"
- 如何对孩子说”不“
- 如何对老板说"不"?
- 如何对上司说“不”
- 对加班坚决说不!
- 程序员要勇于说不
- 程序员要勇于说不
- 程序员要勇于说不
- 程序员要勇于说不
- 程序员要勇于说不
- 程序员要勇于说不
- ant 使用
- java中synchronized的用法详解
- 类的继承、类的多态
- 平时函数积累
- Berkeley DB迁移到PostgreSQL遇到的问题
- datanitro:python程序员可以对 excel VBA 说不
- 你的生活重心是什么?——读《高效能人士的七个习惯》
- VI使用笔记(多用才是王道)
- 静静的在心里承受所有的伤痛和不快乐
- js 变量类型和作用域
- XHTML语法基础
- 赵雅智_java 多线程(2)之线程同步
- csdn教程
- 如果字符串一的所有字符按其在字符串中的顺序出现在另外一个字符串二中,则字符串一称之为字符串二的子串。