<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Python Excels &#187; dan</title>
	<atom:link href="http://www.pythonexcels.com/author/dan/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pythonexcels.com</link>
	<description>Data Mining with Excel and Python</description>
	<lastBuildDate>Mon, 08 Feb 2010 03:56:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A User Friendly Experience</title>
		<link>http://www.pythonexcels.com/2010/02/a-user-friendly-experience/</link>
		<comments>http://www.pythonexcels.com/2010/02/a-user-friendly-experience/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 03:56:05 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.pythonexcels.com/?p=263</guid>
		<description><![CDATA[Let&#8217;s be honest for a second, when was the last time you saw a Windows user running something from the command prompt?   Well, I do it occasionally, but I can&#8217;t say I remember seeing a non-IT person using the command prompt recently.  So if you&#8217;re going to offer your users a Windows [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s be honest for a second, when was the last time you saw a Windows user running something from the command prompt?   Well, I do it occasionally, but I can&#8217;t say I remember seeing a non-IT person using the command prompt recently.  So if you&#8217;re going to offer your users a Windows program, you better give them an icon to click and let them drag stuff onto it.  And if something goes wrong, you better have a decent error message.  This post will take the Pivot Table generation script developed in the <a href="http://www.pythonexcels.com/2009/12/extending-pivot-table-data/">last post</a> and turn it into a user friendly Windows program with better flexibility and improved user experience.  </p>
<p>The scripts developed previously could be run at the command line or by double clicking on the icon for the script line this.</p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2010/02/20100207_commandexe1.png" alt="20100207_commandexe" title="20100207_commandexe" width="470" height="155" class="alignnone size-full wp-image-264" /></p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2010/02/20100207_erpicon.png" alt="20100207_erpicon" title="20100207_erpicon" width="157" height="134" class="alignnone size-full wp-image-265" /></p>
<p>This works because the input file name, ABCDCatering.xls, is hard coded within the script.  In the real world, your users will have folders containing dozens of randomly named spreadsheets.  If a user accidentally provides a corrupt spreadsheet, the program should keep cranking through the other files and let the user recover the damaged file later.  The script developed in the last post will need some enhancements to make it more user friendly, including:</p>
<ul>
<li>Provide support for multiple randomly named input spreadsheets</li>
<li>Add some simple message boxes and drag-and-drop support</li>
<li>Improve the error checking and error recovery to give the user feedback when something goes wrong</li>
</ul>
<p>To keep things concise, this version of the script only allows the user to run the program by dragging and dropping files onto the program icon.   Enhancing the script to also support command line operation is left as an exercise for the user. Let&#8217;s work through each of the usability issues below: </p>
<p><strong>Multiple File Support</strong><br />
As I mentioned, Windows XP/Vista/7 users typically don&#8217;t interact with the command prompt. Instead, programs are run by clicking on their icons, either from the desktop, a folder, or the Start menu.  A user specifies spreadsheets or document files by opening them in the application or dragging them onto the program icon on the desktop or in the Explorer window. You can also add the file names after the program name at the command prompt if needed. </p>
<p>To process multiple files, the program needs to process command line args, which are already conveniently available in the <code>sys.argv</code> list. Note that the first argument <code>sys.argv[0]</code> is used for the script name.  The <code>runexcel</code> function is modified to pass sys.argv to the runexcel function, which loops through each of the input files.</p>
<pre class="brush: python; first-line: 222">
if __name__ == "__main__":
    runexcel(sys.argv)
</pre>
<pre class="brush: python; first-line: 69">
for fname in args[1:]:
    # Process spreadsheet files
</pre>
<p>The <code>for</code> loop wraps the <code>wb = excel.Workbooks.Open(fname)</code> call, the <code>wb.SaveAs()</code> call, and everything in between so each workbook is processed within the loop.  After the loop finishes, a check for errors is made.  If any errors occurred a warning and message box are issued. </p>
<p><strong>Primitive GUI Support</strong><br />
Adding message boxes and providing basic drag-and-drop support adds a level of familiarity for Windows users.  Python supports a large number of GUI frameworks, see <a href="http://wiki.python.org/moin/GuiProgramming">http://wiki.python.org/moin/GuiProgramming</a> for a comprehensive list.  Building a complete graphic interface for this script is beyond the scope of this article, and isn&#8217;t really necessary anyway.  Instead, you can add support for simple message boxes using the MessageBoxA function built into Windows.  The basic pattern for calling a message box using this technique is to <code>import ctypes</code> and call <code>windll.user32.MessageBoxA</code>:</p>
<pre class="brush: python;">
from ctypes import *
windll.user32.MessageBoxA(None,&quot;My Message Box&quot;,&quot;Program Name&quot;,0)
</pre>
<p>This simple code produces a message box with the text &#8220;My Message Box&#8221;, an OK button, and &#8220;Program Name&#8221; as the top banner.  When Python encounters <code>windll.user32.MessageBoxA()</code>, program execution pauses until the user clicks the OK button. </p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2010/02/20100207_messagebox.png" alt="20100207_messagebox" title="20100207_messagebox" width="132" height="107" class="alignnone size-full wp-image-266" /></p>
<p><strong>Improve Error Checking</strong><br />
Lots of problems can happen when reading user spreadsheet data.  The user can forget to specify an input file.  They could try to have the script read a Word document or other non-spreadsheet file type.  The spreadsheet might be corrupted.  You need to bulletproof your script and guard against potential issues, both known and unknown.  </p>
<p>Previous versions of the script made limited use of the <code>try/except</code> pattern to catch errors. </p>
<pre class="brush: python;">
try:
    wb = excel.Workbooks.Open('ABCDCatering.xls')
except:
    print &quot;Failed to open spreadsheet ABCDCatering.xls&quot;
    sys.exit(1)
</pre>
<p>erppivotdragdrop.py makes more liberal use of <code>try/except</code>, wrapping more of the program code in the <code>try</code> block.  If an error occurs, it can be handled more cleanly with nice warning messages.  The downside of using <code>try/except</code> is that you lose the traceback message telling you where the error occurred.  To get this information back, use the <code>traceback</code> module and the <code>traceback.print_exc()</code> function.  One usage is to call <code>traceback.print_exc()</code> in the <code>except</code> block like this:</p>
<pre class="brush: python;">
import traceback
try:
  a = 1/0
except:
  # Do error recovery
  traceback.print_exc()
</pre>
<p>Now exceptions are caught, handled, and a more detailed traceback is still available. </p>
<p><strong>Running the script</strong><br />
Let&#8217;s test out the script.  First, copy the script to the desktop and drag the ABCDCatering.xls spreadsheet onto the icon.  Python starts running in the command window and begins processing the file you dragged.  If everything ran successfully, you&#8217;ll see a series of messages and the &#8220;Finished&#8221; message box.  </p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2010/02/20100207_noerror.png" alt="20100207_noerror" title="20100207_noerror" width="600" height="318" class="alignnone size-full wp-image-267" /></p>
<p>If a problem occurred, a message is displayed in the command window.  At the end of the run, the message box is displayed letting you know that something bad happened and that you should review the error messages.  </p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2010/02/20100207_haserror.png" alt="20100207_haserror" title="20100207_haserror" width="600" height="318" class="alignnone size-full wp-image-268" /></p>
<p>Here is the completed script, also available on <a href="http://github.com/pythonexcels/examples">GitHub</a></p>
<pre class="brush: python;">
#
# erppivotdragdrop.py:
# Load raw EPR data, clean up header info,
# insert additional data fields and build 5 pivot tables
# Support drag and drop of multiple spreadsheets
#
import win32com.client as win32
win32c = win32.constants
import sys
import itertools
import re
import traceback
from ctypes import *

tablecount = itertools.count(1)

def addpivot(wb,sourcedata,title,filters=(),columns=(),
             rows=(),sumvalue=(),sortfield=&quot;&quot;):
    &quot;&quot;&quot;Build a pivot table using the provided source location data
    and specified fields
    &quot;&quot;&quot;
    newsheet = wb.Sheets.Add()
    newsheet.Cells(1,1).Value = title
    newsheet.Cells(1,1).Font.Size = 16

    # Build the Pivot Table
    tname = &quot;PivotTable%d&quot;%tablecount.next()

    pc = wb.PivotCaches().Add(SourceType=win32c.xlDatabase,
                                 SourceData=sourcedata)
    pt = pc.CreatePivotTable(TableDestination=&quot;%s!R4C1&quot;%newsheet.Name,
                             TableName=tname,
                             DefaultVersion=win32c.xlPivotTableVersion10)
    wb.Sheets(newsheet.Name).Select()
    wb.Sheets(newsheet.Name).Cells(3,1).Select()
    for fieldlist,fieldc in ((filters,win32c.xlPageField),
                            (columns,win32c.xlColumnField),
                            (rows,win32c.xlRowField)):
        for i,val in enumerate(fieldlist):
            wb.ActiveSheet.PivotTables(tname).PivotFields(val).Orientation = fieldc
            wb.ActiveSheet.PivotTables(tname).PivotFields(val).Position = i+1

    wb.ActiveSheet.PivotTables(tname).AddDataField(
        wb.ActiveSheet.PivotTables(tname).PivotFields(sumvalue[7:]),
        sumvalue,
        win32c.xlSum)
    if len(sortfield) != 0:
        wb.ActiveSheet.PivotTables(tname).PivotFields(sortfield[0]).AutoSort(sortfield[1], sumvalue)
    newsheet.Name = title

    # Uncomment the next command to limit output file size, but make sure
    # to click Refresh Data on the PivotTable toolbar to update the table
    #
    # newsheet.PivotTables(tname).SaveData = False

    return tname

def runexcel(args):
    &quot;&quot;&quot;Open the spreadsheet ABCDCatering.xls, clean it up,
    and add pivot tables
    &quot;&quot;&quot;
    sawerror = False
    print &quot;Running erppivotdragdrop&quot;
    if len(args) == 1:
        windll.user32.MessageBoxA(None,&quot;Error: Please drag at least one Excel file&quot;,&quot;erppivotdragdrop&quot;,0)
        sys.exit(1)
    try:
        excel = win32.gencache.EnsureDispatch('Excel.Application')
        for fname in args[1:]:
            if not re.search(r'\.(?i)xlsx?$',fname):
                print &quot;Error: File %s doesn't seem to be an Excel file, expecting .xls or .xlsx file&quot; % fname
                sawerror = True
                continue
            if not re.match('[A-Za-z]:',fname):
                print &quot;Error: erppivotdragdrop doesn't support command line execution&quot;
                print &quot;       Please drag and drop the Excel file onto the program icon&quot;
                sawerror = True
                continue
            print &quot;Processing %s&quot; % fname
            try:
                wb = excel.Workbooks.Open(fname)
            except:
                print &quot;Failed to open Excel file %s, skipping&quot; % fname
                sawerror = True
                continue

            try:
                ws = wb.Sheets('Sheet1')
            except:
                print &quot;Failed to open Sheet 'Sheet1' in file %s, skipping&quot; % fname
                wb.Close()
                sawerror = True
                continue

            xldata = ws.UsedRange.Value
            newdata = []
            for row in xldata:
                if len(row) == 13 and row[-1] is not None:
                    newdata.append(list(row))
            lasthdr = &quot;Col A&quot;
            for i,field in enumerate(newdata[0]):
                if field is None:
                    newdata[0][i] = lasthdr + &quot; Name&quot;
                else:
                    lasthdr = newdata[0][i]

            logolookup = {'Applied Materials':'AMAT', 'Electronic Arts':'EA',
                          'Hewlett-Packard':'HP', 'KLA-Tencor':'KLA'}
            if (&quot;Company Name&quot; in newdata[0]):
                cindx = newdata[0].index(&quot;Company Name&quot;)
                newdata[0][cindx+1:cindx+1] = [&quot;Logo Name&quot;]
                for rcnt in range(1,len(newdata)):
                    if newdata[rcnt][cindx] in logolookup:
                        newdata[rcnt][cindx+1:cindx+1] = [logolookup[newdata[rcnt][cindx]]]
                    else:
                        newname = newdata[rcnt][cindx].split()[0]
                        newdata[rcnt][cindx+1:cindx+1] = [newname]
                        logolookup[newdata[rcnt][cindx]] = newname

            foodlookup = {'Caesar Salad':'Salad', 'Cheese Pizza':'Pizza',
                          'Cheeseburger':'Burger', 'Chocolate Sundae':'Dessert',
                          'Churro':'Snack', 'Hamburger':'Burger', 'Hot Dog':'HotDog',
                          'Pepperoni Pizza':'Pizza', 'Potato Chips':'Snack',
                          'Soda':'Drink'}
            if (&quot;Food Name&quot; in newdata[0]):
                cindx = newdata[0].index(&quot;Food Name&quot;)
                newdata[0][cindx+1:cindx+1] = [&quot;Food Category&quot;]
                for rcnt in range(1,len(newdata)):
                    if newdata[rcnt][cindx] in foodlookup:
                        newdata[rcnt][cindx+1:cindx+1] = [foodlookup[newdata[rcnt][cindx]]]
                    else:
                        newdata[rcnt][cindx+1:cindx+1] = ['UNDEFINED']

            rowcnt = len(newdata)
            colcnt = len(newdata[0])
            wsnew = wb.Sheets.Add()
            wsnew.Range(wsnew.Cells(1,1),wsnew.Cells(rowcnt,colcnt)).Value = newdata
            wsnew.Columns.AutoFit()

            src = &quot;%s!R1C1:R%dC%d&quot;%(wsnew.Name,rowcnt,colcnt)

            # What were the total sales in each of the last four quarters?
            addpivot(wb,src,
                     title=&quot;Sales by Quarter&quot;,
                     filters=(),
                     columns=(),
                     rows=(&quot;Fiscal Quarter&quot;,),
                     sumvalue=&quot;Sum of Net Booking&quot;,
                     sortfield=())

            # What are the sales for each food item in each quarter?
            addpivot(wb,src,
                     title=&quot;Sales by Food Item&quot;,
                     filters=(),
                     columns=(&quot;Food Name&quot;,),
                     rows=(&quot;Fiscal Quarter&quot;,),
                     sumvalue=&quot;Sum of Net Booking&quot;,
                     sortfield=())

            # Who were the top 10 customers for ABCD Catering in 2009?
            addpivot(wb,src,
                     title=&quot;Top 10 Customers&quot;,
                     filters=(),
                     columns=(),
                     rows=(&quot;Company Name&quot;,),
                     sumvalue=&quot;Sum of Net Booking&quot;,
                     sortfield=(&quot;Company Name&quot;,win32c.xlDescending))

            # Who was the highest producing sales rep for the year?
            addpivot(wb,src,
                     title=&quot;Top Sales Reps&quot;,
                     filters=(),
                     columns=(),
                     rows=(&quot;Sales Rep Name&quot;,&quot;Company Name&quot;),
                     sumvalue=&quot;Sum of Net Booking&quot;,
                     sortfield=(&quot;Sales Rep Name&quot;,win32c.xlDescending))

            # What food item had the highest unit sales in Q4?
            ptname = addpivot(wb,src,
                     title=&quot;Unit Sales by Food&quot;,
                     filters=(&quot;Fiscal Quarter&quot;,),
                     columns=(),
                     rows=(&quot;Food Name&quot;,),
                     sumvalue=&quot;Sum of Quantity&quot;,
                     sortfield=(&quot;Food Name&quot;,win32c.xlDescending))
            wb.Sheets(&quot;Unit Sales by Food&quot;).PivotTables(ptname).PivotFields(&quot;Fiscal Quarter&quot;).CurrentPage = &quot;2009-Q4&quot;

            # What food category had the highest unit sales in Q4?
            ptname = addpivot(wb,src,
                     title=&quot;Unit Sales by Food Category&quot;,
                     filters=(&quot;Fiscal Quarter&quot;,),
                     columns=(),
                     rows=(&quot;Food Category&quot;,),
                     sumvalue=&quot;Sum of Quantity&quot;,
                     sortfield=(&quot;Food Category&quot;,win32c.xlDescending))
            wb.Sheets(&quot;Unit Sales by Food Category&quot;).PivotTables(ptname).PivotFields(&quot;Fiscal Quarter&quot;).CurrentPage = &quot;2009-Q4&quot;

            outfname = re.sub('(?i)\.xlsx?','_new',fname)
            try:
                if int(float(excel.Version)) &gt;= 12:
                    wb.SaveAs(outfname+'.xlsx',win32c.xlOpenXMLWorkbook)
                    print &quot;Wrote %s&quot; % outfname+'.xlsx'
                else:
                    wb.SaveAs(outfname+'.xls')
                    print &quot;Wrote %s&quot; % outfname+'.xls'
            except:
                print &quot;Error: Problem during file save&quot;
                sawerror = True
            wb.Close()
        if sawerror:
            print &quot;Errors occurred, please check the above messages&quot;
            windll.user32.MessageBoxA(None,&quot;Error: Problems occurred, please check them and try again&quot;,&quot;erppivotdragdrop&quot;,0)
        else:
            print &quot;Finished&quot;
            windll.user32.MessageBoxA(None,&quot;Finished&quot;,&quot;erppivotdragdrop&quot;,0)
    except:
        traceback.print_exc()
        print &quot;Errors occurred, please check the above messages&quot;
        windll.user32.MessageBoxA(None,&quot;Error: Problems occurred, please check them and try again&quot;,&quot;erppivotdragdrop&quot;,0)
    excel.Application.Quit()

if __name__ == &quot;__main__&quot;:
    runexcel(sys.argv)
</pre>
<p><strong>Prerequisites</strong><br />
Python (refer to <a href="http://www.python.org">http://www.python.org</a>)</p>
<p>Win32 Python module (refer to <a href="http://sourceforge.net/projects/pywin32">http://sourceforge.net/projects/pywin32</a>)</p>
<p>Microsoft Excel (refer to <a href="http://office.microsoft.com/excel">http://office.microsoft.com/excel</a>)</p>
<p><strong>Source Files and Scripts</strong><br />
Source for the program erppivotextended.py and spreadsheet file ABCDCatering.xls are available at<br /><a href="http://github.com/pythonexcels/examples">http://github.com/pythonexcels/examples</a></p>
<p>Thanks &#8212; Dan</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pythonexcels.com/2010/02/a-user-friendly-experience/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending Pivot Table Data</title>
		<link>http://www.pythonexcels.com/2009/12/extending-pivot-table-data/</link>
		<comments>http://www.pythonexcels.com/2009/12/extending-pivot-table-data/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 19:20:17 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[ERP]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.pythonexcels.com/?p=248</guid>
		<description><![CDATA[As shown in the last post, automating pivot table generation with Python and Excel helps you quickly clean up a spreadsheet, organize data and build useful reports in very few lines of code.  Another useful data preparation technique is to build new columns of information based on the available data.  For example, you [...]]]></description>
			<content:encoded><![CDATA[<p>As shown in the <a href="http://www.pythonexcels.com/2009/11/automating-pivot-tables-with-python/">last post</a>, automating pivot table generation with Python and Excel helps you quickly clean up a spreadsheet, organize data and build useful reports in very few lines of code.  Another useful data preparation technique is to build new columns of information based on the available data.  For example, you could add an industry segment column to group company names by industry, or add an item type column to group sales items by category.  While Excel does have some functions to help with adding new data fields, automation with Python eliminates the tedium of clicking column names and entering formulas.</p>
<p>Excel does provide a function for calculating new values within a pivot table.  One example is extending a pivot table containing pricing and quantity data to compute an average selling price.  For example, given the table below:</p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2009/12/20091203_salesbyqtr.png" alt="20091203_salesbyqtr" title="20091203_salesbyqtr" width="392" height="323" class="alignnone size-full wp-image-249" /></p>
<p>a new label called &#8220;ASP&#8221;, which is the Net Booking divided by the Quantity, can be added quickly and easily with Excel&#8217;s Calculated Field capability.</p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2009/12/20091203_calcfield.png" alt="20091203_calcfield" title="20091203_calcfield" width="549" height="612" class="alignnone size-full wp-image-250" /></p>
<p>This feature is handy for adding labels on the fly that require a simple calculation.  </p>
<p>In other cases, deriving the new field may not be so simple, yet needs to be performed each time the spreadsheet is updated.  Python can programmatically add new data fields to the source table so that the data is ready for viewing whenever the pivot table is opened.</p>
<p>The script developed last time automated the data cleanup and pivot table generation tasks.  Doing some further analysis based on the output spreadsheet, I created a chart of the Top 10 Customers for ABCD Catering: </p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2009/12/20091203_top10chart.png" alt="20091203_top10chart" title="20091203_top10chart" width="550" height="353" class="alignnone size-full wp-image-251" /></p>
<p>Note that some of the company names are 15 characters or longer in length and occupy much of the chart space.  It would be nice to have a shorter &#8220;nickname&#8221; for each company that could be used in the charts.  One solution is to cut and paste the pivot table data, then modify the Company Name information by hand.  Unfortunately, this would be very tedious.  Another approach is to automate the process in the script and create a new column derived from a comprehensive reference table of company names and nicknames.  The downside is that maintaining the list could be an issue as the business grows and the list of customers grows longer.  A third method is to create an algorithm that uses the first word in the company name wherever possible, and uses a defined nickname for other special cases.  &#8220;Sun Microsystems&#8221; becomes &#8220;Sun&#8221; and &#8220;Cisco Systems&#8221; becomes &#8220;Cisco&#8221;, while other company names such as &#8220;Hewlett-Packard&#8221; could be listed in a lookup with a nickname such as &#8220;HP&#8221;.  The snippet below shows how this is done.</p>
<pre class="brush: python;">
logolookup = {'Applied Materials':'AMAT', 'Electronic Arts':'EA',
              'Hewlett-Packard':'HP', 'KLA-Tencor':'KLA'}
if (&quot;Company Name&quot; in newdata[0]):
    cindx = newdata[0].index(&quot;Company Name&quot;)
    newdata[0][cindx+1:cindx+1] = [&quot;Logo Name&quot;]
    for rcnt in range(1,len(newdata)):
        if newdata[rcnt][cindx] in logolookup:
            newdata[rcnt][cindx+1:cindx+1] = [logolookup[newdata[rcnt][cindx]]]
        else:
            newname = newdata[rcnt][cindx].split()[0]
            newdata[rcnt][cindx+1:cindx+1] = [newname]
            logolookup[newdata[rcnt][cindx]] = newname
</pre>
<p>This code begins with  a simple lookup for company names and can be easily extended as special case company names are added.  Next, the column location of the &#8220;Company Name&#8221; field is identified and the new header &#8220;Logo Name&#8221; is inserted after &#8220;Company Name&#8221; in the list using the <code>list[index:index]</code> construct.  The <code>for</code> loop iterates over each row in the table, checking whether the company name for that row exists in the <code>logolookup</code> dictionary, then inserting the abbreviated name.  If not found, then the original company name is <code>split()</code> into words and the first word used as the new abbreviated name.  Finally, the <code>logolookup</code> dictionary is updated with the new abbreviated name.  </p>
<p>After running the program, the new column &#8220;Logo Name&#8221; has been inserted after &#8220;Company Name&#8221; and contains the shortened company names.</p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2009/12/20091203_withlogo.png" alt="20091203_withlogo" title="20091203_withlogo" width="431" height="273" class="alignnone size-full wp-image-252" /></p>
<p>The new &#8220;Logo Name&#8221; column can be used in the previous pivot table and chart, replacing the &#8220;Company Name&#8221; field and producing a cleaner chart with less area used for displaying company name information.<br />
<img src="http://www.pythonexcels.com/wp-content/uploads/2009/12/20091203_top10wlogo.png" alt="20091203_top10wlogo" title="20091203_top10wlogo" width="550" height="354" class="alignnone size-full wp-image-253" /> </p>
<p>Another use of this technique is to add a label for &#8220;Food Category&#8221; based on the type of food purchased.  For example, the food items sold by ABCD Catering are: Caesar Salad, Cheese Pizza, Cheeseburger, Chocolate Sundae, Churro, Hamburger, Hot Dog, Pepperoni Pizza, Potato Chips and Soda.  Let&#8217;s say that your manager wants to track the sales of different food categories, such as Burger, Dessert, HotDog, Drink, Pizza, Salad and Snack.  Using the same technique outlined above, this code will add a column for Food Category with the appropriate entry for each food item: </p>
<pre class="brush: python;">
foodlookup = {'Caesar Salad':'Salad', 'Cheese Pizza':'Pizza',
              'Cheeseburger':'Burger', 'Chocolate Sundae':'Dessert',
              'Churro':'Snack', 'Hamburger':'Burger', 'Hot Dog':'HotDog',
              'Pepperoni Pizza':'Pizza', 'Potato Chips':'Snack',
              'Soda':'Drink'}
if (&quot;Food Name&quot; in newdata[0]):
    cindx = newdata[0].index(&quot;Food Name&quot;)
    newdata[0][cindx+1:cindx+1] = [&quot;Food Category&quot;]
    for rcnt in range(1,len(newdata)):
        if newdata[rcnt][cindx] in foodlookup:
            newdata[rcnt][cindx+1:cindx+1] = [foodlookup[newdata[rcnt][cindx]]]
        else:
            newdata[rcnt][cindx+1:cindx+1] = ['UNDEFINED']
</pre>
<p>If a food item is not found in the lookup, the category is labeled UNDEFINED.  This is an indication that there is a problem with the script and the lookup for food categories needs to be extended. </p>
<p>The section of the script which creates the pivot tables can be easily extended to build a new table based on the newly created label &#8220;Food Category&#8221;:</p>
<pre class="brush: python;">
# What food category had the highest unit sales in Q4?
ptname = addpivot(wb,src,
         title=&quot;Unit Sales by Food Category&quot;,
         filters=(&quot;Fiscal Quarter&quot;,),
         columns=(),
         rows=(&quot;Food Category&quot;,),
         sumvalue=&quot;Sum of Quantity&quot;,
         sortfield=(&quot;Food Category&quot;,win32c.xlDescending))
wb.Sheets(&quot;Unit Sales by Food Category&quot;).PivotTables(ptname).PivotFields(&quot;Fiscal Quarter&quot;).CurrentPage = &quot;2009-Q4&quot;
</pre>
<p>Based on the output spreadsheet, the best selling food category in Q4 based on quantity is &#8220;Snack&#8221;, with sales of 13700 units.  </p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2009/12/20091203_foodcategory.png" alt="20091203_foodcategory" title="20091203_foodcategory" width="333" height="381" class="alignnone size-full wp-image-254" /></p>
<p>Here is the completed script, also available on <a href="http://github.com/pythonexcels/examples">GitHub</a></p>
<pre class="brush: python;">
#
# erppivotextended.py:
# Load raw EPR data, clean up header info,
# insert additional data fields and build 5 pivot tables
#
import win32com.client as win32
win32c = win32.constants
import sys
import itertools
tablecount = itertools.count(1)

def addpivot(wb,sourcedata,title,filters=(),columns=(),
             rows=(),sumvalue=(),sortfield=&quot;&quot;):
    &quot;&quot;&quot;Build a pivot table using the provided source location data
    and specified fields
    &quot;&quot;&quot;
    newsheet = wb.Sheets.Add()
    newsheet.Cells(1,1).Value = title
    newsheet.Cells(1,1).Font.Size = 16

    # Build the Pivot Table
    tname = &quot;PivotTable%d&quot;%tablecount.next()

    pc = wb.PivotCaches().Add(SourceType=win32c.xlDatabase,
                                 SourceData=sourcedata)
    pt = pc.CreatePivotTable(TableDestination=&quot;%s!R4C1&quot;%newsheet.Name,
                             TableName=tname,
                             DefaultVersion=win32c.xlPivotTableVersion10)
    wb.Sheets(newsheet.Name).Select()
    wb.Sheets(newsheet.Name).Cells(3,1).Select()
    for fieldlist,fieldc in ((filters,win32c.xlPageField),
                            (columns,win32c.xlColumnField),
                            (rows,win32c.xlRowField)):
        for i,val in enumerate(fieldlist):
            wb.ActiveSheet.PivotTables(tname).PivotFields(val).Orientation = fieldc
            wb.ActiveSheet.PivotTables(tname).PivotFields(val).Position = i+1

    wb.ActiveSheet.PivotTables(tname).AddDataField(
        wb.ActiveSheet.PivotTables(tname).PivotFields(sumvalue[7:]),
        sumvalue,
        win32c.xlSum)
    if len(sortfield) != 0:
        wb.ActiveSheet.PivotTables(tname).PivotFields(sortfield[0]).AutoSort(sortfield[1], sumvalue)
    newsheet.Name = title

    # Uncomment the next command to limit output file size, but make sure
    # to click Refresh Data on the PivotTable toolbar to update the table
    # newsheet.PivotTables(tname).SaveData = False

    return tname

def runexcel():
    &quot;&quot;&quot;Open the spreadsheet ABCDCatering.xls, clean it up,
    and add pivot tables
    &quot;&quot;&quot;
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    #excel.Visible = True
    try:
        wb = excel.Workbooks.Open('ABCDCatering.xls')
    except:
        print &quot;Failed to open spreadsheet ABCDCatering.xls&quot;
        sys.exit(1)
    ws = wb.Sheets('Sheet1')
    xldata = ws.UsedRange.Value
    newdata = []
    for row in xldata:
        if len(row) == 13 and row[-1] is not None:
            newdata.append(list(row))
    lasthdr = &quot;Col A&quot;
    for i,field in enumerate(newdata[0]):
        if field is None:
            newdata[0][i] = lasthdr + &quot; Name&quot;
        else:
            lasthdr = newdata[0][i]

    logolookup = {'Applied Materials':'AMAT', 'Electronic Arts':'EA',
                  'Hewlett-Packard':'HP', 'KLA-Tencor':'KLA'}
    if (&quot;Company Name&quot; in newdata[0]):
        cindx = newdata[0].index(&quot;Company Name&quot;)
        newdata[0][cindx+1:cindx+1] = [&quot;Logo Name&quot;]
        for rcnt in range(1,len(newdata)):
            if newdata[rcnt][cindx] in logolookup:
                newdata[rcnt][cindx+1:cindx+1] = [logolookup[newdata[rcnt][cindx]]]
            else:
                newname = newdata[rcnt][cindx].split()[0]
                newdata[rcnt][cindx+1:cindx+1] = [newname]
                logolookup[newdata[rcnt][cindx]] = newname

    foodlookup = {'Caesar Salad':'Salad', 'Cheese Pizza':'Pizza',
                  'Cheeseburger':'Burger', 'Chocolate Sundae':'Dessert',
                  'Churro':'Snack', 'Hamburger':'Burger', 'Hot Dog':'HotDog',
                  'Pepperoni Pizza':'Pizza', 'Potato Chips':'Snack',
                  'Soda':'Drink'}
    if (&quot;Food Name&quot; in newdata[0]):
        cindx = newdata[0].index(&quot;Food Name&quot;)
        newdata[0][cindx+1:cindx+1] = [&quot;Food Category&quot;]
        for rcnt in range(1,len(newdata)):
            if newdata[rcnt][cindx] in foodlookup:
                newdata[rcnt][cindx+1:cindx+1] = [foodlookup[newdata[rcnt][cindx]]]
            else:
                newdata[rcnt][cindx+1:cindx+1] = ['UNDEFINED']

    rowcnt = len(newdata)
    colcnt = len(newdata[0])
    wsnew = wb.Sheets.Add()
    wsnew.Range(wsnew.Cells(1,1),wsnew.Cells(rowcnt,colcnt)).Value = newdata
    wsnew.Columns.AutoFit()

    src = &quot;%s!R1C1:R%dC%d&quot;%(wsnew.Name,rowcnt,colcnt)

    # What were the total sales in each of the last four quarters?
    addpivot(wb,src,
             title=&quot;Sales by Quarter&quot;,
             filters=(),
             columns=(),
             rows=(&quot;Fiscal Quarter&quot;,),
             sumvalue=&quot;Sum of Net Booking&quot;,
             sortfield=())

    # What are the sales for each food item in each quarter?
    addpivot(wb,src,
             title=&quot;Sales by Food Item&quot;,
             filters=(),
             columns=(&quot;Food Name&quot;,),
             rows=(&quot;Fiscal Quarter&quot;,),
             sumvalue=&quot;Sum of Net Booking&quot;,
             sortfield=())

    # Who were the top 10 customers for ABCD Catering in 2009?
    addpivot(wb,src,
             title=&quot;Top 10 Customers&quot;,
             filters=(),
             columns=(),
             rows=(&quot;Company Name&quot;,),
             sumvalue=&quot;Sum of Net Booking&quot;,
             sortfield=(&quot;Company Name&quot;,win32c.xlDescending))

    # Who was the highest producing sales rep for the year?
    addpivot(wb,src,
             title=&quot;Top Sales Reps&quot;,
             filters=(),
             columns=(),
             rows=(&quot;Sales Rep Name&quot;,&quot;Company Name&quot;),
             sumvalue=&quot;Sum of Net Booking&quot;,
             sortfield=(&quot;Sales Rep Name&quot;,win32c.xlDescending))

    # What food item had the highest unit sales in Q4?
    ptname = addpivot(wb,src,
             title=&quot;Unit Sales by Food&quot;,
             filters=(&quot;Fiscal Quarter&quot;,),
             columns=(),
             rows=(&quot;Food Name&quot;,),
             sumvalue=&quot;Sum of Quantity&quot;,
             sortfield=(&quot;Food Name&quot;,win32c.xlDescending))
    wb.Sheets(&quot;Unit Sales by Food&quot;).PivotTables(ptname).PivotFields(&quot;Fiscal Quarter&quot;).CurrentPage = &quot;2009-Q4&quot;

    # What food category had the highest unit sales in Q4?
    ptname = addpivot(wb,src,
             title=&quot;Unit Sales by Food Category&quot;,
             filters=(&quot;Fiscal Quarter&quot;,),
             columns=(),
             rows=(&quot;Food Category&quot;,),
             sumvalue=&quot;Sum of Quantity&quot;,
             sortfield=(&quot;Food Category&quot;,win32c.xlDescending))
    wb.Sheets(&quot;Unit Sales by Food Category&quot;).PivotTables(ptname).PivotFields(&quot;Fiscal Quarter&quot;).CurrentPage = &quot;2009-Q4&quot;

    if int(float(excel.Version)) &gt;= 12:
        wb.SaveAs('newABCDCatering.xlsx',win32c.xlOpenXMLWorkbook)
    else:
        wb.SaveAs('newABCDCatering.xls')
    excel.Application.Quit()

if __name__ == &quot;__main__&quot;:
    runexcel()
</pre>
<p><strong>Prerequisites</strong><br />
Python (refer to <a href="http://www.python.org">http://www.python.org</a>)</p>
<p>Win32 Python module (refer to <a href="http://sourceforge.net/projects/pywin32">http://sourceforge.net/projects/pywin32</a>)</p>
<p>Microsoft Excel (refer to <a href="http://office.microsoft.com/excel">http://office.microsoft.com/excel</a>)</p>
<p><strong>Source Files and Scripts</strong><br />
Source for the program erppivotextended.py and spreadsheet file ABCDCatering.xls are available at<br /><a href="http://github.com/pythonexcels/examples">http://github.com/pythonexcels/examples</a></p>
<p>Thanks &#8212; Dan</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pythonexcels.com/2009/12/extending-pivot-table-data/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Automating Pivot Tables with Python</title>
		<link>http://www.pythonexcels.com/2009/11/automating-pivot-tables-with-python/</link>
		<comments>http://www.pythonexcels.com/2009/11/automating-pivot-tables-with-python/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 03:49:01 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[ERP]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.pythonexcels.com/?p=237</guid>
		<description><![CDATA[In the last post I explained the basic concept behind Pivot Tables and provided some examples.  Pivot tables are an easy-to-use tool to derive some basic business intelligence from your data.  As discussed last time, there are occasions when you&#8217;ll need to do interactive data mining by changing column and row fields.  [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.pythonexcels.com/2009/11/introducing-pivot-tables">last post</a> I explained the basic concept behind Pivot Tables and provided some examples.  Pivot tables are an easy-to-use tool to derive some basic business intelligence from your data.  As discussed last time, there are occasions when you&#8217;ll need to do interactive data mining by changing column and row fields.  But in my experience, it&#8217;s handy to have my favorite reports built automatically, with the reports ready to go as soon as I open the spreadsheet.  In this post I&#8217;ll develop and explain the code to create a set of pivot tables automatically in worksheet. </p>
<p>The goal of this exercise is to automate the generation of pivot tables from the last post, and save them to a new Excel file.</p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2009/11/20091123_reports.png" alt="20091123_reports" title="20091123_reports" width="550" height="655" class="alignnone size-full wp-image-242" /></p>
<p>I started with the file <code>newABCDCatering.xls</code> from the previous post and record the macro to create this simple pivot table showing Net Bookings by Sales Rep and Food Name for the last four quarters.<br />
<img src="http://www.pythonexcels.com/wp-content/uploads/2009/11/20091123_setup.png" alt="20091123_setup" title="20091123_setup" width="550" height="396" class="alignnone size-full wp-image-238" /></p>
<p>Captured in Excel 2007, the recorded macro looks like this: </p>
<pre class="brush: vb;">
Sub Macro1()
'
' Macro1 Macro
'

'
    Selection.CurrentRegion.Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        &quot;Sheet2!R1C1:R791C13&quot;, Version:=xlPivotTableVersion10).CreatePivotTable _
        TableDestination:=&quot;Sheet3!R3C1&quot;, TableName:=&quot;PivotTable1&quot;, DefaultVersion _
        :=xlPivotTableVersion10
    Sheets(&quot;Sheet3&quot;).Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Fiscal Year&quot;)
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Fiscal Quarter&quot;)
        .Orientation = xlColumnField
        .Position = 1
    End With
    With ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Sales Rep Name&quot;)
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Food Name&quot;)
        .Orientation = xlRowField
        .Position = 2
    End With
    ActiveSheet.PivotTables(&quot;PivotTable1&quot;).AddDataField ActiveSheet.PivotTables( _
        &quot;PivotTable1&quot;).PivotFields(&quot;Net Booking&quot;), &quot;Sum of Net Booking&quot;, xlSum
End Sub
</pre>
<p>The post <a href="http://www.pythonexcels.com/2009/10/mapping-excel-vb-macros-to-python/">Mapping Excel VB Macros to Python</a> covered a technique for recording a Visual Basic macro and porting it to Python.  Using that approach, you could simply turn on the macro recorder and generate all the required tables, producing a long script with lots of redundancy.  A better approach is to build a general purpose function that can be used over and over to generate the pivot tables.</p>
<p>Looking at the macro, you see lines specifying the <code>Orientation</code> of the field name, such as <code>.Orientation = xlRowField</code> and <code>.Orientation = xlColumnField</code>.  A pivot table has four basic areas for fields: </p>
<ul>
<li>Report Filter (<code>.Orientation = xlPageField</code>)</li>
<li>Column area (<code>.Orientation = xlColumnField</code>)</li>
<li>Row area (<code>.Orientation = xlRowField</code>)</li>
<li>Values area (<code>PivotTables().AddDataField()</code>)</li>
</ul>
<p>Each of these supports multiple fields (column fields for <code>Sales Rep Name</code> and <code>Food Name</code> were added in the example).  The ordering of the fields changes the appearance of the table.  </p>
<p>A general pattern should be apparent in this macro.  First, the pivot table is created with the <code>ActiveWorkbook.PivotCaches.Create()</code> statement.  Next, the columns and rows are configured with a series of <code>ActiveSheet.PivotTables("PivotTable1").PivotFields()</code> statements.  Finally, the field used in the <code>Values</code> section of the table is configured using the <code>ActiveSheet.PivotTables("PivotTable1").AddDataField</code> statement.  The general purpose function will need to contain all of these constructs.  Note the parts that can&#8217;t be hard-coded: the source of the data, <code>"Sheet2!R1C1:R791C13"</code>, and destination for the table, <code>"Sheet3!R3C1"</code> need to be determined based on the characteristics of the source data and can&#8217;t be hard coded in the general solution.</p>
<p>In Python, this pattern can be reduced to the following loop that covers fields for the Report Filter, Columns and Rows:</p>
<pre class="brush: python;">
def addpivot(wb,sourcedata,title,filters=(),columns=(),
             rows=(),sumvalue=(),sortfield=&quot;&quot;):
    &quot;&quot;&quot;Build a pivot table using the provided source location data
    and specified fields
    &quot;&quot;&quot;
    ...
    for fieldlist,fieldc in ((filters,win32c.xlPageField),
                            (columns,win32c.xlColumnField),
                            (rows,win32c.xlRowField)):
        for i,val in enumerate(fieldlist):
            wb.ActiveSheet.PivotTables(tname).PivotFields(val).Orientation = fieldc
        wb.ActiveSheet.PivotTables(tname).PivotFields(val).Position = i+1
    ...
</pre>
<p>Processing the Values field is more or less copied from the Visual Basic.  To keep things simple in this example, this code is limited to adding &#8220;Sum of&#8221; values only, and doesn&#8217;t handle other Summarize Value functions such as Count, Min, Max, etc.</p>
<pre class="brush: python;">
    wb.ActiveSheet.PivotTables(tname).AddDataField(
        wb.ActiveSheet.PivotTables(tname).PivotFields(sumvalue[7:]),
        sumvalue,
        win32c.xlSum)
</pre>
<p>The actual values for <code>filters</code>,<code>columns</code> and <code>rows</code> in the function are defined in the call to the function.  The complete function creates a new sheet within the workbook, then adds an empty pivot table to the sheet and builds the table using the field information provided.  For example, to answer the question: <i>What were the total sales in each of the last four quarters?</i>, the pivot table is built with the following call to the <code>addpivot</code> function:</p>
<pre class="brush: python;">
# What were the total sales in each of the last four quarters?
addpivot(wb,src,
         title=&quot;Sales by Quarter&quot;,
         filters=(),
         columns=(),
         rows=(&quot;Fiscal Quarter&quot;,),
         sumvalue=&quot;Sum of Net Booking&quot;,
         sortfield=())
</pre>
<p>which defines a pivot table using the row header &#8220;Fiscal Quarter&#8221; and data value &#8220;Sum of Net Booking&#8221;.  The title &#8220;Sales by Quarter&#8221; is used to name the sheet itself.</p>
<p>To make the output spreadsheet more understandable, the title parameter passed into the function and used as a title in each worksheet and as the tab name.</p>
<p><img src="http://www.pythonexcels.com/wp-content/uploads/2009/11/20091123_titletabsbq.png" alt="20091123_titletabsbq" title="20091123_titletabsbq" width="389" height="363" class="alignnone size-full wp-image-243" /></p>
<p>The complete script is shown below.  Caveats:</p>
<ul>
<li>This script has been modified to run on both Excel 2007 and Excel 2003 and has been tested on those versions.</li>
<li>Adding pivot tables increases the size of the output Excel file, which can be mitigated by disabling caching of pivot table data.  Line 48 of the script contains the command <code>newsheet.PivotTables(tname).SaveData = False</code>, which has been commented out.  Uncommenting this command will reduce the size of the output Excel file, but will require that the pivot table be refreshed before use by clicking on Refresh Data on the PivotTable toolbar.</li>
</ul>
<pre class="brush: python;">
#
# erpdatapivot.py:
# Load raw EPR data, clean up header info and
# build 5 pivot tables
#
import win32com.client as win32
win32c = win32.constants
import sys
import itertools
tablecount = itertools.count(1)

def addpivot(wb,sourcedata,title,filters=(),columns=(),
             rows=(),sumvalue=(),sortfield=&quot;&quot;):
    &quot;&quot;&quot;Build a pivot table using the provided source location data
    and specified fields
    &quot;&quot;&quot;
    newsheet = wb.Sheets.Add()
    newsheet.Cells(1,1).Value = title
    newsheet.Cells(1,1).Font.Size = 16

    # Build the Pivot Table
    tname = &quot;PivotTable%d&quot;%tablecount.next()

    pc = wb.PivotCaches().Add(SourceType=win32c.xlDatabase,
                                 SourceData=sourcedata)
    pt = pc.CreatePivotTable(TableDestination=&quot;%s!R4C1&quot;%newsheet.Name,
                             TableName=tname,
                             DefaultVersion=win32c.xlPivotTableVersion10)
    wb.Sheets(newsheet.Name).Select()
    wb.Sheets(newsheet.Name).Cells(3,1).Select()
    for fieldlist,fieldc in ((filters,win32c.xlPageField),
                            (columns,win32c.xlColumnField),
                            (rows,win32c.xlRowField)):
        for i,val in enumerate(fieldlist):
            wb.ActiveSheet.PivotTables(tname).PivotFields(val).Orientation = fieldc
            wb.ActiveSheet.PivotTables(tname).PivotFields(val).Position = i+1

    wb.ActiveSheet.PivotTables(tname).AddDataField(
        wb.ActiveSheet.PivotTables(tname).PivotFields(sumvalue[7:]),
        sumvalue,
        win32c.xlSum)
    if len(sortfield) != 0:
        wb.ActiveSheet.PivotTables(tname).PivotFields(sortfield[0]).AutoSort(sortfield[1], sumvalue)
    newsheet.Name = title

    # Uncomment the next command to limit output file size, but make sure
    # to click Refresh Data on the PivotTable toolbar to update the table
    # newsheet.PivotTables(tname).SaveData = False

    return tname

def runexcel():
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    #excel.Visible = True
    try:
        wb = excel.Workbooks.Open('ABCDCatering.xls')
    except:
        print &quot;Failed to open spreadsheet ABCDCatering.xls&quot;
        sys.exit(1)
    ws = wb.Sheets('Sheet1')
    xldata = ws.UsedRange.Value
    newdata = []
    for row in xldata:
        if len(row) == 13 and row[-1] is not None:
            newdata.append(list(row))
    lasthdr = &quot;Col A&quot;
    for i,field in enumerate(newdata[0]):
        if field is None:
            newdata[0][i] = lasthdr + &quot; Name&quot;
        else:
            lasthdr = newdata[0][i]
    rowcnt = len(newdata)
    colcnt = len(newdata[0])
    wsnew = wb.Sheets.Add()
    wsnew.Range(wsnew.Cells(1,1),wsnew.Cells(rowcnt,colcnt)).Value = newdata
    wsnew.Columns.AutoFit()

    src = &quot;%s!R1C1:R%dC%d&quot;%(wsnew.Name,rowcnt,colcnt)

    # What were the total sales in each of the last four quarters?
    addpivot(wb,src,
             title=&quot;Sales by Quarter&quot;,
             filters=(),
             columns=(),
             rows=(&quot;Fiscal Quarter&quot;,),
             sumvalue=&quot;Sum of Net Booking&quot;,
             sortfield=())

    # What are the sales for each food item in each quarter?
    addpivot(wb,src,
             title=&quot;Sales by Food Item&quot;,
             filters=(),
             columns=(&quot;Food Name&quot;,),
             rows=(&quot;Fiscal Quarter&quot;,),
             sumvalue=&quot;Sum of Net Booking&quot;,
             sortfield=())

    # Who were the top 10 customers for ABCD Catering in 2009?
    addpivot(wb,src,
             title=&quot;Top 10 Customers&quot;,
             filters=(),
             columns=(),
             rows=(&quot;Company Name&quot;,),
             sumvalue=&quot;Sum of Net Booking&quot;,
             sortfield=(&quot;Company Name&quot;,win32c.xlDescending))

    # Who was the highest producing sales rep for the year?
    addpivot(wb,src,
             title=&quot;Top Sales Reps&quot;,
             filters=(),
             columns=(),
             rows=(&quot;Sales Rep Name&quot;,&quot;Company Name&quot;),
             sumvalue=&quot;Sum of Net Booking&quot;,
             sortfield=(&quot;Sales Rep Name&quot;,win32c.xlDescending))

    # What food item had the highest unit sales in Q4?
    ptname = addpivot(wb,src,
             title=&quot;Unit Sales by Food&quot;,
             filters=(&quot;Fiscal Quarter&quot;,),
             columns=(),
             rows=(&quot;Food Name&quot;,),
             sumvalue=&quot;Sum of Quantity&quot;,
             sortfield=(&quot;Food Name&quot;,win32c.xlDescending))
    wb.Sheets(&quot;Unit Sales by Food&quot;).PivotTables(ptname).PivotFields(&quot;Fiscal Quarter&quot;).CurrentPage = &quot;2009-Q4&quot;

    if int(float(excel.Version)) &gt;= 12:
        wb.SaveAs('newABCDCatering.xlsx',win32c.xlOpenXMLWorkbook)
    else:
        wb.SaveAs('newABCDCatering.xls')
    excel.Application.Quit()

if __name__ == &quot;__main__&quot;:
    runexcel()
</pre>
<p><strong>Prerequisites</strong><br />
Python (refer to <a href="http://www.python.org">http://www.python.org</a>)</p>
<p>Microsoft Excel (refer to <a href="http://office.microsoft.com/excel">http://office.microsoft.com/excel</a>)</p>
<p><strong>Source Files and Scripts</strong><br />
Source for the program erpdatapivot.py and input spreadsheet file ABCDCatering.xls are available at<br /><a href="http://github.com/pythonexcels/examples">http://github.com/pythonexcels/examples</a></p>
<p>Thanks &#8212; Dan</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pythonexcels.com/2009/11/automating-pivot-tables-with-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
