Monday 15 January 2018

More XLwings

So I wanted to do a little bit more on XLwings. I am hoping to also achieve this using some free gif creation software that I have found. So I am hoping to cover: 
  • Creating tabs 
  • Switching between tabs
  • Setting cell values 
  • hiding tabs 
  • Refreshing pivot table
The starting point: The picture below shows the starting point. We have a Data tab and a Pivot tab. There is some data in the data tab which has been pivoted on the pivot tab. The data on the data tab has stupidly small column widths and although we will hide it we still want it to look good so will fit those properly. 



So the basic steps I am going to take (as you can see in the code and screenshots) are:

  • import required modules 
  • open the workbook 
  • put in some sleep statements so I can more easily record the gifs
  • commands to switch between workbooks
  • create a new workbook and hide it
  • enter some data on the data tab and auto-fit it. 
  • select the pivot tab and refresh the data
The gif below shows it in action, watch carefully and you can see all the actions I describe above. 


The code:


4 comments:

  1. Great post! Is there anyway to use Xlwings to create pivot table?

    ReplyDelete
    Replies
    1. Not that I am aware of. You can create a pivot table in an Excel template, open the template and then refresh the pivot table. I have used this approach before but been a while (since I checked this blog and since I have used XLWings).

      Delete
  2. How do we change pivot source data for existing pivot table??

    ReplyDelete
  3. Hey,

    Is there a way to refresh all pivot tables at once?

    ReplyDelete