Skip to content

Instantly share code, notes, and snippets.

@drnextgis
Last active December 14, 2015 19:59
Show Gist options
  • Save drnextgis/5140507 to your computer and use it in GitHub Desktop.
Save drnextgis/5140507 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import os
import sys
import win32com.client as win32
from regions import regions
def build_registry(root_dir, build_header=True):
registry_path = os.path.join(root_dir, 'registry.xls')
# Create Excel app
xl = win32.Dispatch('Excel.Application')
# Remove previos registry
if os.path.exists(registry_path):
os.remove(registry_path)
if build_header:
wb = xl.Workbooks.Add()
sh = wb.ActiveSheet
sh.Range(sh.Cells(1,1), sh.Cells(2,1)).Merge()
sh.Range(sh.Cells(1,2), sh.Cells(2,2)).Merge()
sh.Range(sh.Cells(1,3), sh.Cells(2,3)).Merge()
sh.Range(sh.Cells(1,4), sh.Cells(2,4)).Merge()
sh.Range(sh.Cells(1,5), sh.Cells(2,5)).Merge()
sh.Range(sh.Cells(1,6), sh.Cells(2,6)).Merge()
sh.Range(sh.Cells(1,7), sh.Cells(2,7)).Merge()
sh.Range(sh.Cells(1,8), sh.Cells(2,8)).Merge()
sh.Range(sh.Cells(1,9), sh.Cells(2,9)).Merge()
sh.Range(sh.Cells(1,10), sh.Cells(1,15)).Merge()
sh.Range(sh.Cells(1,16), sh.Cells(1,21)).Merge()
sh.Range(sh.Cells(1,22), sh.Cells(1,27)).Merge()
sh.Range(sh.Cells(1,28), sh.Cells(1,33)).Merge()
sh.Range(sh.Cells(1,34), sh.Cells(1,39)).Merge()
sh.Range(sh.Cells(1,40), sh.Cells(2,40)).Merge()
sh.Range(sh.Cells(1,41), sh.Cells(2,41)).Merge()
sh.Cells(1,1).Value = u'Предприятие'
sh.Cells(1,2).Value = u'Район'
sh.Cells(1,3).Value = u'ИНН'
sh.Cells(1,4).Value = u'БИК'
sh.Cells(1,5).Value = u'Р/Сч'
sh.Cells(1,6).Value = u'Среднемесячная заработная плата (доход) 1 работника в 2012 г., тыс. руб.'
sh.Cells(1,7).Value = u'Урожайность зерновых и зернобобовых культур в первоначально оприходованном весе на 1 га посевной площади в 2012 г., ц/га'
sh.Cells(1,8).Value = u'Осуществляет деятельность в области животноводства в настоящее время'
sh.Cells(1,9).Value = u'Посевная площадь всех сельскохозяйственных культур, га'
sh.Cells(1,10).Value = u'Сахарная свекла'
sh.Cells(2,10).Value = u'Площадь, га'
sh.Cells(2,11).Value = u'Ставка субсидии из федерального бюджета, руб.'
sh.Cells(2,12).Value = u'Ставка субсидии из краевого бюджета, руб.'
sh.Cells(2,13).Value = u'Коэффициент среднемесячной заработной платы в 2012 г.'
sh.Cells(2,14).Value = u'Начислено из федерального бюджета, руб.'
sh.Cells(2,15).Value = u'Начислено из краевого бюджета, руб.'
sh.Cells(1,16).Value = u'Кормовые культуры (за исключением многолетних трав)'
sh.Cells(2,16).Value = u'Площадь, га'
sh.Cells(2,17).Value = u'Ставка субсидии из федерального бюджета, руб.'
sh.Cells(2,18).Value = u'Ставка субсидии из краевого бюджета, руб.'
sh.Cells(2,19).Value = u'Коэффициент среднемесячной заработной платы в 2012 г.'
sh.Cells(2,20).Value = u'Начислено из федерального бюджета, руб.'
sh.Cells(2,21).Value = u'Начислено из краевого бюджета, руб.'
sh.Cells(1,22).Value = u'Многолетние травы'
sh.Cells(2,22).Value = u'Площадь, га'
sh.Cells(2,23).Value = u'Ставка субсидии из федерального бюджета, руб.'
sh.Cells(2,24).Value = u'Ставка субсидии из краевого бюджета, руб.'
sh.Cells(2,25).Value = u'Коэффициент среднемесячной заработной платы в 2012 г.'
sh.Cells(2,26).Value = u'Начислено из федерального бюджета, руб.'
sh.Cells(2,27).Value = u'Начислено из краевого бюджета, руб.'
sh.Cells(1,28).Value = u'Зерновые и зернобобовые культуры'
sh.Cells(2,28).Value = u'Площадь, га'
sh.Cells(2,29).Value = u'Ставка субсидии из федерального бюджета, руб.'
sh.Cells(2,30).Value = u'Ставка субсидии из краевого бюджета, руб.'
sh.Cells(2,31).Value = u'Коэффициент среднемесячной заработной платы в 2012 г.'
sh.Cells(2,32).Value = u'Начислено из федерального бюджета, руб.'
sh.Cells(2,33).Value = u'Начислено из краевого бюджета, руб.'
sh.Cells(1,34).Value = u'Прочие культуры'
sh.Cells(2,34).Value = u'Площадь, га'
sh.Cells(2,35).Value = u'Ставка субсидии из федерального бюджета, руб.'
sh.Cells(2,36).Value = u'Ставка субсидии из краевого бюджета, руб.'
sh.Cells(2,37).Value = u'Коэффициент среднемесячной заработной платы в 2012 г.'
sh.Cells(2,38).Value = u'Начислено из федерального бюджета, руб.'
sh.Cells(2,39).Value = u'Начислено из краевого бюджета, руб.'
sh.Cells(1,40).Value = u'Начислено из федерального бюджета, руб.'
sh.Cells(1,41).Value = u'Начислено из краевого бюджета, руб.'
else:
# Open template
wb = xl.Workbooks.Open(os.path.join('C:/gush/subs','template.xls'))
sh = wb.ActiveSheet
start_row = 3
sh.Columns(3).NumberFormat = "@"
sh.Columns(4).NumberFormat = "@"
sh.Columns(5).NumberFormat = "@"
# Populate registry
for root, dirs, files in os.walk(root_dir):
files = [fi for fi in files if (fi.endswith('.xls') or fi.endswith('.xlsx'))]
for f in files:
sub_wb = xl.Workbooks.Open(os.path.join(root,f))
sub_sh = sub_wb.ActiveSheet
sh.Cells(start_row,1).Value = sub_sh.Cells(3,3).Value # Предприятие
print sub_sh.Cells(3,3).Value
sh.Cells(start_row,2).Value = regions[int(sub_sh.Cells(7,3).Value)] # Район
sh.Cells(start_row,3).Value = sub_sh.Cells(7,5).Value # ИНН
sh.Cells(start_row,4).Value = sub_sh.Cells(10,5).Value # БИК
sh.Cells(start_row,5).Value = sub_sh.Cells(11,5).Value # Р/Сч
sh.Cells(start_row,6).Value = {1: u'ниже 6,5',
2: u'от 6,5 до 8,5',
3: u'от 8,5 до 10',
4: u'свыше 10',
5: u'нет наёмных работников'}.get(int(sub_sh.Cells(10,3).Value)) # Зарплата
sh.Cells(start_row,7).Value = {1: u'до 4 (включительно)', 2: u'более 4'}.get(int(sub_sh.Cells(11,3).Value)) # Урожайность зерновых
sh.Cells(start_row,8).Value = {1: u'Нет', 2:u'Да'}.get(int(sub_sh.Cells(12,3).Value)) # Животноводство
sh.Cells(start_row,9).Value = sub_sh.Cells(17,2).Value # Посевная площадь
sh.Cells(start_row,10).Value = sub_sh.Cells(18,2).Value # Сахарная свекла
sh.Cells(start_row,11).Value = sub_sh.Cells(18,3).Value
sh.Cells(start_row,12).Value = sub_sh.Cells(18,4).Value
sh.Cells(start_row,13).Value = sub_sh.Cells(18,6).Value
sh.Cells(start_row,14).Value = sub_sh.Cells(18,5).Value
sh.Cells(start_row,15).Value = sub_sh.Cells(18,7).Value
sh.Cells(start_row,16).Value = sub_sh.Cells(19,2).Value # Кормовые
sh.Cells(start_row,17).Value = sub_sh.Cells(19,3).Value
sh.Cells(start_row,18).Value = sub_sh.Cells(19,4).Value
sh.Cells(start_row,19).Value = sub_sh.Cells(19,6).Value
sh.Cells(start_row,20).Value = sub_sh.Cells(19,5).Value
sh.Cells(start_row,21).Value = sub_sh.Cells(19,7).Value
sh.Cells(start_row,22).Value = sub_sh.Cells(20,2).Value # Многолетние травы
sh.Cells(start_row,23).Value = sub_sh.Cells(20,3).Value
sh.Cells(start_row,24).Value = sub_sh.Cells(20,4).Value
sh.Cells(start_row,25).Value = sub_sh.Cells(20,6).Value
sh.Cells(start_row,26).Value = sub_sh.Cells(20,5).Value
sh.Cells(start_row,27).Value = sub_sh.Cells(20,7).Value
sh.Cells(start_row,28).Value = sub_sh.Cells(21,2).Value # Зерновые и зернобобовые
sh.Cells(start_row,29).Value = sub_sh.Cells(21,3).Value
sh.Cells(start_row,30).Value = sub_sh.Cells(21,4).Value
sh.Cells(start_row,31).Value = sub_sh.Cells(21,6).Value
sh.Cells(start_row,32).Value = sub_sh.Cells(21,5).Value
sh.Cells(start_row,33).Value = sub_sh.Cells(21,7).Value
sh.Cells(start_row,34).Value = sub_sh.Cells(22,2).Value # Прочие культуры
sh.Cells(start_row,35).Value = sub_sh.Cells(22,3).Value
sh.Cells(start_row,36).Value = sub_sh.Cells(22,4).Value
sh.Cells(start_row,37).Value = sub_sh.Cells(22,6).Value
sh.Cells(start_row,38).Value = sub_sh.Cells(22,5).Value
sh.Cells(start_row,39).Value = sub_sh.Cells(22,7).Value
sh.Cells(start_row,40).Value = sub_sh.Cells(23,5).Value
sh.Cells(start_row,41).Value = sub_sh.Cells(23,7).Value
start_row+=1
sub_wb.Close(False)
# Save new registry
wb.SaveAs(registry_path)
wb.Close(False)
xl.Application.Quit()
if __name__ == '__main__':
build_registry(sys.argv[1], False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment