[Tutor] Interoperating with Excel, was Re: Questions

Albert-Jan Roskam sjeik_appie at hotmail.com
Mon Apr 8 14:38:06 EDT 2019



On 7 Apr 2019 14:07, Peter Otten <__peter__ at web.de> wrote:

Diana Katz wrote:

> 1) Can you use python from excel? Or just export to excel?
> 2) I am trying to see if there's a way using python to automate all of
> this work that I need to do. I have to collect quarterly segment data for
> hundreds of public companies and go back at least 12-16 quarters. We use
> an aggregator like factset and they actually don't have this option
> available in an automated way. So I'm trying to see if there's a way to
> build this. Basically, I get my data from sec.gov and they have
> interactive data - they even have the data in excel (though it's a messy
> file and hard to read). I attached some of the steps and the data that i'd
> want to see. Basically i'd want the excel to look like:
> old to new quarters - going back 12 to 16 quarters (more if possible but
> not if it will stop the project).
>  Columns: 3/31/2017, 6/30/2017, 9/30/17, 12/31/17, 3/313/2018...
> Rows:
> Sales for segment A
> Sales for Segment b
> Sales for SEgment C
> …(for as many segments as they have)
>
> Earnings for Segment A
> .Earnings for Segment B
>
> Depreciation for Segment A
> Depreciation for Segment B
> Depreciation for Segment C...

These look like "pivot tables" which are well supported by Excel.
I expect that this is easy to automate with a little bit of Basic.

Of course you can build these tables with a Python script if you feel more
comfortable in Python. Then either write them into csv files ("comma
separated value", supported by the standard library)

https://docs.python.org/3/library/csv.html

which can be read by Excel -- or use a dedicated library. Google came up
with

https://xlsxwriter.readthedocs.io/


===>> There's xlrd for xls files, openpyxl for xlsx files. And pandas can also read excel, and you can use groupby, pivottable etc. there:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html. With the win32com library you can also automate Excel (Windows only).

Excel is evil, especially if the files are made by humans and come from different sources.






More information about the Tutor mailing list