首页 > 编程知识 正文

python中macro(python里面)

时间:2023-12-19 00:42:52 阅读:317506 作者:FFAN

本文目录一览:

VBA和Python难度对比有哪些?

VBA和Python难度对比有:把使用目的和场景从完成一些“小任务”上升到做一个完整的、可重复使用、考虑用户体验的project,则VBA的难度会上身。

VBA主要基于和嵌入在以MS Office为核心的使用环境,Python的使用和应用范围则更广,可以处理和VBA一样的数据分析,数据可视化,也可以进行编程开发等。

如果是在MS Office的环境下,例如MS Excel, Word,那么主要目的大概率上是为了提高日常处理数据的效率以及提高自动化程度。那么,我觉得Python会比较难学。

根据我自己在工作中的经验,VBA使用频率最高的“知识”不外乎Loop, IF statement,以及一些编程的基本原理和常识,例如declare variables, data type等。仔细学一下这些基本概念,上手VBA还是比较容易的。而且,别忘了VBA还有一个神助手,Macro (宏),它可以自动生成VBA code,可以直接复制过来用,也可以根据需自己的要进行修改。

学习方法:

1. 明确目标

对于零基础的学员而言,要明确你学习Python仅仅是为了满足好奇心?还是有工作需要,比如办公自动化的需要,转行的需要。不同的目标,所选用的学习内容和相应的学习方法也就会有所不同。

2. 选择内容

对于零基础学员,如要要达到使用Python的需要,那都是需要从Python基础知识进行系统学习的。

下面是参考的《零基础学Python》教材,为便于没有变成基础的同学学习,整理了下python基本知识点及对小白学员友好的学习顺序。

关于Python中的一段为Python脚本添加行号脚本

C语言有__LINE__来表示源代码的当前行号,经常在记录日志时使用。Python如何获取源代码的当前行号?

The C Language has the __LINE__ macro, which is wildly used in logging, presenting the current line of the source file. And how to get the current line of a Python source file?

exception输出的函数调用栈就是个典型的应用:

A typical example is the output of function call stack when an exception:

python代码

File "D:workspacePythonsrclanglineno.py", line 19, in module

afunc()

File "D:workspacePythonsrclanglineno.py", line 15, in afunc

errmsg = 1/0

ZeroDivisionError: integer division or modulo by zero

那么我们就从错误栈的输出入手,traceback模块中:

Now that, Let's begin with the output of an exception call stack, in the traceback module:

python代码

def print_stack(f=None, limit=None, file=None):

"""Print a stack trace from its invocation point.

The optional 'f' argument can be used to specify an alternate

stack frame at which to start. The optional 'limit' and 'file'

arguments have the same meaning as for print_exception().

"""

if f is None:

try:

raise ZeroDivisionError

except ZeroDivisionError:

f = sys.exc_info()[2].tb_frame.f_back

print_list(extract_stack(f, limit), file)

def print_list(extracted_list, file=None):

"""Print the list of tuples as returned by extract_tb() or

extract_stack() as a formatted stack trace to the given file."""

if file is None:

file = sys.stderr

for filename, lineno, name, line in extracted_list:

_print(file,

' File "%s", line %d, in %s' % (filename,lineno,name))

if line:

_print(file, ' %s' % line.strip())

traceback模块构造一个ZeroDivisionError,并通过sys模块的exc_info()来获取运行时上下文。我们看到,所有的秘密都在tb_frame中,这是函数调用栈中的一个帧。

traceback constructs an ZeroDivisionError, and then call the exc_info() of the sys module to get runtime context. There, all the secrets hide in the tb_frame, this is a frame of the function call stack.

对,就是这么简单!只要我们能找到调用栈frame对象即可获取到行号!因此,我们可以用同样的方法来达到目的,我们自定义一个lineno函数:

Yes, It's so easy! If only a frame object we get, we can get the line number! So we can have a similar implemetation to get what we want, defining a function named lineno:

python代码

import sys

def lineno():

frame = None

try:

raise ZeroDivisionError

except ZeroDivisionError:

frame = sys.exc_info()[2].tb_frame.f_back

return frame.f_lineno

def afunc():

# if error

print "I have a problem! And here is at Line: %s"%lineno()

是否有更方便的方法获取到frame对象?当然有!

Is there any other way, perhaps more convinient, to get a frame object? Of course YES!

python代码

def afunc():

# if error

print "I have a proble! And here is at Line: %s"%sys._getframe().f_lineno

类似地,通过frame对象,我们还可以获取到当前文件、当前函数等信息,就像C语音的__FILE__与__FUNCTION__一样。其实现方式,留给你们自己去发现。

Thanks to the frame object, similarly, we can also get current file and current function name, just like the __FILE__ and __FUNCTION__ macros in C. Debug the frame object, you will get the solutions.

Pycharm中如何使用宏快速编写代码

Pycharm中的宏功能可以帮助我们快速的插入常用的代码,那么如何使用宏功能呢?下面我给大家分享一下。

工具/材料

Pycharm

首先打开Pycharm软件,新建一个python文件,如下图所示

接下来点击顶部的Edit菜单,在下拉菜单中选择Macros下面的Start Macro Recording选项,如下图所示

然后我们开始在python文件中写入一些常用的代码,我这里简单的写了个输出语句,大家可以根据自己的需求编写,如下图所示

编写完内容以后,继续点击顶部的Edit菜单,这次选择Macros下面的Stop Macro Recording选项,如下图所示

接下来会弹出一个给宏命名的界面,我们给刚才录制的宏起一个名字点击OK按钮即可,如下图所示

然后我们点击顶部的File菜单,接着选择下拉菜单中的Settings选项,如下图所示

在弹出的Settings界面中定位到Keymap选项, 然后右键选择刚才创建的宏,点击Add Keyboard Shortcut选项,如下图所示

接着我们给宏添加调用的快捷键,如下图所示,直接按向添加的键即可

最后我们在python文件中就可以输入定义的快捷键调用宏来快速的编写代码了,如下图所示

这个python程序的入口在哪?

可以自己定义入口(main)

在代码最后面加上

if __name__=='__main__':

    M = Macro()    #实例化类文件

    M.ExecuteBlock()  #调用类文件的方法

这个实际上类似于C 或者 java 的main()

假设这个文件叫做mac.py

命令行里执行

python mac.py  

#就会执行if __name__=='__main__': 下面的代码

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。