Exporting AdWords API call to Excel (CSV)

939 views
Skip to first unread message

John N.

unread,
Apr 23, 2018, 3:55:56 AM4/23/18
to AdWords API Forum
Very new to using API's and Python in general but I was able to generate an AD Performance report using the following:

  report_query = (adwords.ReportQueryBuilder()
           .Select('CampaignName', 'AdGroupName', 'HeadlinePart1', 'HeadlinePart2', 'Description', 'Clicks','Cost','AverageCpc','Labels')
           .From('AD_PERFORMANCE_REPORT')
           .Where('CampaignStatus').EqualTo('ENABLED')
           .Where('Clicks').GreaterThan('0')
           .During('YESTERDAY')
           .Build())

  report_downloader.DownloadReportWithAwql(
      report_query, 'CSV', sys.stdout, skip_report_header=False,
      skip_column_header=False, skip_report_summary=False,
      include_zero_impressions=True)

However, the call prints the output within the cmd prompt and closes. This is probably a very basic question but how would i update the report download section to produce a CSV file? Would i need to pass the information to a Pandas DataFrame and write to excel?

Thanks in advance.

Peter Oliquino (AdWords API Team)

unread,
Apr 23, 2018, 4:36:40 AM4/23/18
to AdWords API Forum
Hi John,

Your current report definition is already set to generate a CSV file as you can see below :

report_downloader.DownloadReportWithAwql(
      report_query, 'CSV', sys.stdout, skip_report_header=False,
      skip_column_header=False, skip_report_summary=False,
      include_zero_impressions=True)

The file should then be downloaded straight to the folder you specified in your PATH :

PATH = '/<your folder>/report_download.csv'

This being said, could you confirm if you are not yet able to see the generated CSV file when you run the report request?

Best regards,
Peter
AdWords API Team

John N.

unread,
Apr 23, 2018, 12:54:05 PM4/23/18
to AdWords API Forum

Hi Peter, 

Running the following:

import sys
from googleads import adwords

# Specify where to download the file here.
PATH = 'C:/Users/Desktop/Python/supernova/report_download.csv'


def main(client):
  # Initialize appropriate service.
  report_downloader = client.GetReportDownloader(version='v201802')

  # Construct query and get all campaigns.
  report_query = (adwords.ReportQueryBuilder()
           .Select('CampaignName', 'AdGroupName', 'HeadlinePart1', 'HeadlinePart2', 'Description', 'Clicks','Cost','AverageCpc','Labels')
           .From('AD_PERFORMANCE_REPORT')
           .Where('CampaignStatus').EqualTo('ENABLED')
           .Where('Clicks').GreaterThan('0')
           .During('YESTERDAY')
           .Build())

  report_downloader.DownloadReportWithAwql(
      report_query, 'CSV', PATH, skip_report_header=False,
      skip_column_header=False, skip_report_summary=False,
      include_zero_impressions=True)

produces the following error:
AttributeError: 'str' object has no attribute 'write'

If i keep the sys.stdout vs. PATH, then the file fails to write to the path

Best,

Peter Oliquino (AdWords API Team)

unread,
Apr 24, 2018, 12:06:19 AM4/24/18
to AdWords API and Google Ads API Forum
Hi John,

Since this appears to be more related to the Python client library, you may get in touch with the library owners via this link for further assistance.

Daniel Klaus

unread,
Jan 27, 2019, 10:45:45 PM1/27/19
to AdWords API and Google Ads API Forum
I was able to solve this by redirecting stdout to a file in advance of the DownloadReportWithAwql() call:

output_file = 'report_download.csv'

# Create report query.
report_query = (adwords.ReportQueryBuilder()
.Select('CampaignId', 'CampaignName', 'CostPerConversion', 'Conversions', 'Ctr', 'Cost',
'Clicks', 'Impressions', 'AverageCpc')
.From('CAMPAIGN_PERFORMANCE_REPORT')
.Where('CampaignStatus').In('ENABLED', 'PAUSED', 'REMOVED')
.During('20180101,20180201')
.Build())

# You can provide a file object to write the output to. For this
# demonstration, redirect stdout to a file object.
with open(output_file, 'w'):
sys.stdout = open(output_file, 'w')
report_downloader.DownloadReportWithAwql(
report_query, 'CSV', sys.stdout, skip_report_header=True,
skip_column_header=False, skip_report_summary=True,
include_zero_impressions=True)
Reply all
Reply to author
Forward
0 new messages