Python Forum
Can Python be used as a macro language for excel, Word, etc.?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can Python be used as a macro language for excel, Word, etc.?
#1
I currently use Visual Basic for Applications (VBA) as the macro language for Excel and Word. It's a horrible language. Can Python be used instead?
Reply
#2
Yes python can be used.
please post a small excel file with output requirements, for an example.

In the mean time, you can look through excel related packages here: https://pypi.org/search/?q=excel
also look at openpyxl: https://pypi.org/project/openpyxl/
and it's doc page: https://openpyxl.readthedocs.io/en/stable/
CynthiaMoore likes this post
Reply
#3
(Aug-19-2022, 10:39 PM)Larz60+ Wrote: Yes python can be used.
please post a small excel file with output requirements, for an example.

In the mean time, you can look through excel related packages here: https://pypi.org/search/?q=excel
also look at openpyxl: https://pypi.org/project/openpyxl/
and it's doc page: https://openpyxl.readthedocs.io/en/stable/

I have attached an Excel workbook that uses a VBA function to format a time interval in the smallest units that is <= 1. Sheet 2 has the actual worksheet code. It will not work because the VBA function is in an add-in. Sheet 1 has the literal values.

The VBA function syntax is:

=FmtTime(pTime, [pDP|1], [pInUnits|"Days"])
  • pTime is the time interval to be formatted
  • pDP is the number of decimal places to return
  • pInUnits is the units of pTime

Thanks


.xlsx   Format Time.xlsx (Size: 12.84 KB / Downloads: 113)
Reply
#4
The attachment appears to be a php file, not excel.
Reply
#5
(Aug-20-2022, 10:33 PM)Larz60+ Wrote: The attachment appears to be a php file, not excel.

How can that be. When I download it, it is a xlsx file.

Here it is in a OneDrive folder:

https://1drv.ms/x/s!ArBLbKVM2K_HiPYwByt_kvi2d3XmGw
Reply
#6
Hi @CynthiaMoore

Maybe I'm not fully understanding what it is that you're trying to do?

But surely, given the data from Sheet1, would the output in column F, rows 5 through 13, formatted as 'Days to 2 decimal places' not be this?:

Output:
0.00 0.00 0.08 1.00 1.00 29.35 31.35 365.00 366.00
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#7
(Aug-21-2022, 04:14 AM)rob101 Wrote: Hi @CynthiaMoore

Maybe I'm not fully understanding what it is that you're trying to do?

But surely, given the data from Sheet1, would the output in column F, rows 5 through 13, formatted as 'Days to 2 decimal places' not be this?:

Output:
0.00 0.00 0.08 1.00 1.00 29.35 31.35 365.00 366.00

Formatted as days, yes. But I don't want it formatted as days. I want it formatted in the smallest units that will be >= 1. Intervals from 0 to 59.5 seconds will be formatted as seconds. Since the default units is days, that means multiplying the value by 24*60*60. Intervals from 1 to 59.5 minutes will be formatted as minutes by multiplying by 24*60. Intervals from 1 to 23.5 hours will be formatted as hours by multiplying by 24. Intervals from 1 to 29.5 days will be formatted as days. No adjustment needed. Intervals between 1 and 11.5 months will be formatted as months by dividing by 30. And intervals greater than 1 year will be formatted as years by dividing by 365.25.

I have added a new column (E) to the table showing the difference (End-Start). The units are days. I want code to convert the values in E to the values in F.

[Image: image.png]

I already VBA that will do it. I am wondering if I can do it in Python. That is, can I write a fmttime function in Python that I can call like I do my VBA function?

Is that any clearer?
Reply
#8
(Aug-21-2022, 05:48 AM)CynthiaMoore Wrote: Is that any clearer?

Yes, very much so. Thank you.

I would like to be able to offer you a Python based solution, but given that I can't and that I think that you can do what you want to do, using a custom style sheet, I offer this:

You'd need to create a new style sheet, with a new style for each duration.

On the new style sheet, create a style called minutes for which the format code could be (for example):
MM:SS" (Mins:Sec)"

... and a style for hours for which the format code could be (again, for example):

HH:MM" (Hrs:Min)"

... and so on, for each time period.

The standard format code (for any given cell) would be SS" (Sec)" with conditional format, for which the rule is...

Condition 1: Cell value between 0.00069540 and 0.04166605 Apply style minutes
Condition 2: Cell value between 0.04167764 and 0.99999679 Apply style hours

... and so on, again, for each time period. If your cells will only ever hold any given time period, then you need only one condition for said cell, but if your cells can hold any given time period, then you'll need as many conditions as there are time periods.

This could read as more complicated than it is in fact; the real work involves (as with any programming project) designing the rules and applying them, but I'm sure that it can be done.

This is all VBA, behind the curtain, but you're using Excel as a 'front-end', if you will.

n.b: the values I have here are:
0.04166605 = 00:59:59
0.99999679 = 23:59:59
CynthiaMoore likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#9
You can also use Python as a scripting language for LibreOffice and OpenOffice.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Has Major Problems as Language Dave 13 3,678 May-20-2022, 10:12 AM
Last Post: stevendaprano
  TIOBE Index for October 2021 - Python programming language number 1! Yoriz 1 1,546 Oct-07-2021, 11:35 PM
Last Post: Larz60+
  a new language based on python Skaperen 0 1,652 Nov-04-2020, 01:54 AM
Last Post: Skaperen
  Is python an interpreted language or a compiled language? GouravDas 1 2,031 Jun-23-2020, 10:38 AM
Last Post: Gribouillis
  what programming language to learn other than python in real mass-production product? Kai 1 2,122 Apr-12-2020, 04:40 AM
Last Post: Larz60+
  Chinese coding language ‘Mulan’ found to be Python fork buran 0 1,727 Jan-22-2020, 04:30 AM
Last Post: buran
  What is the best book for in-depth Python for advanced other-language programmers? user118967 1 2,671 Jun-04-2019, 07:56 PM
Last Post: snippsat
  Coconut - A Functional Language Which Compiles to Python nilamo 2 2,722 Jan-04-2019, 12:01 AM
Last Post: micseydel
  Some Guy Today Told Me Python Isn't A Real Programming Language digitalmatic7 11 10,206 Dec-01-2017, 12:58 PM
Last Post: I_love_py

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020