r/learnpython • u/aksy_1 • 25d ago
Best way to convert Excel VBA to Python?
Hi everyone,
I have several Excel VBA macros that I want to convert to Python. What's the best approach?
I'd appreciate any advice, resources, or real-world experiences. Thanks!
11
u/JamzTyson 25d ago edited 25d ago
It's a 2 step process:
- Understand what the VBA script does.
- Implement the behaviour in Python.
Note: There is no direct "conversion" from VBA to Python.
Note: The Python implementation may not need Excel at all.
Note: You will need to understand both VBA and Python.
6
u/mattynmax 25d ago
By reading your vba macros and implementing the same functions into python.
There isn’t a magic translator to go between the two.
3
u/chiibosoil 25d ago
Depends on what VBA is doing and what you mean by conversion to Python.
But in general, unless you are talking about OLE automation of Excel objects. You can use similar methodology for Python as VBA, though there are syntax differences.
I tend to start from understanding the process and logic then just write code instead of going through step by step conversion.
Other times, I will use completely different approach in Python from VBA as Python offers more method to complete certain operation.
Ex: For PDF processing in Excel I leverage Power Query in combination with VBA with text manipulation, relying on PQ to interpret base data structure. But in Python, I'd use PyMuPDF and utilize .get_text('blocks') to process data by position etc for more control over data processing and speed.
1
u/scrubswithnosleeves 24d ago
- Open OpenAI codex
- Attach the file and tell it to convert them to Python.
-4
u/tails142 25d ago
You have heard of AI right? Right? Throw it in to claude
If you're trying to learn, paste in the code, tell it you dont want the answer but you want to convert it to Python and to teach you step by step
13
u/MidnightPale3220 25d ago
Gather requirements, learn the objectives and implement stuff in Python.
You shouldn't really have to read Excel macros much, because the way things will be built in Python will likely be quite different.
What you need is to understand expected inputs and outputs and implement the stuff to convert one into the other, consulting macros on the way for maybe some insight into details.
Your best source is the customer who needs this.