Python写作excel

时间:2013-09-17 14:24:30

标签: python excel

我想获得以下结构:

                 Day 1   |    2    |   3    | 4       ... Total
stackoverflow                04:00            01:00       05:00
google             05:30               02:00              07:30

但是我的代码,我得到了:

                 Day 1   |    2    |   3    | 4      ...  Total
stackoverflow                04:00                        04:00
google             05:30                                  05:30
stackoverflow                                01:00        01:00
google                               02:00                02:00

我正在运行此查询(given here):

select 
  agenc.name, 
  sum(cast(servic.end_hour as time) - cast(servic.begin_hour as time)) as time_, jobs.description
from 
  services as servic  
  join services_jobs AS jobs ON jobs.id = servic.job_id  
  join agency as agenc ON agenc.id = jobs.agency_id
where  
  extract(month from servic.service_date) = 9 and  
  extract(day from servic.service_date) = 16
group by
  agenc.name

在python中,我执行相同的查询,区别在于查询“extract(来自servic.service_date的日期)”中的“day column”等于循环(从1到31 - > day count )

query_A = cr.fetchall()
    if query_A:
        for values in query_A:
            sheet.write(line_, 0, values[0], style_main) # Writes the name
            sheet.write(line_, 1, values[2], style_main) # Writes the job

            current_value = str(values[1]);
            hours_ = current_value.split(':')[0]
            minutes_ = current_value.split(':')[1]
            total_ = hours_ +':'+ minutes_ # This will get 02:00 - for example

            sheet.write(line_, i + 1, total_, style_main) # write the result
            total_vertical += timedelta(hours=int(hours_), minutes=int(minutes_)) # here should sum the results along the days
            sheet.write(line_, 33, str(total_vertical), style_main) # write it on "total" column    
            line_ += 1

            total_vertical = timedelta(hours=0, minutes=0) # Reset 

有关如何继续获取我想要的结果的任何建议?谢谢!

1 个答案:

答案 0 :(得分:0)

问题已解决,请参阅以下链接中的代码:http://pastebin.com/kcH1bTMp