Thread: How to make a Python script run on Windows?
hi. wrote first real python program on last week, , package run on windows. kindly explain how done?
program takes list of urls in text file, searches websites according regular expression, , prints result excel. also, there 2 modules: first work, , second gui.
program written python3.2 uses following modules: urllib.request, re, win32com.client, pickle, os , tkinter.
thanks.
i going try use cx_freeze. here scripts, first real work , second gui. comments welcome:
code:#!/usr/bin/python3.2 # dostats.py version: 1.2 # # v1.2 adds html class # v1.3 "search , replace" allows multiple searches # # program creates list of players text file, , list # of urls text files. websites searched # player names, , stats each player recorded. stats # written excel spreadsheet. import urllib.request import re win32com.client import dispatch import pickle import os let_to_num = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26} spreadsheet = os.path.join(os.getcwd(), 'test.xlsx') player_filename = "player_list.txt" url_filename = "urls.txt" class player: """a player object representing member of team. instance variables representing player include name & position. stats , stat_cells corresponding tuples contain stats , cells of spreadsheet write stats. other data use in updating stats , writing spreadsheet include regex (regular expression extracting data), spreadsheet name, , worksheet name.""" all_players = [] # keep list of players created def __init__(self, name): """creates player, eg: skater = player("alex ovechkin")""" self.name = name self.regex = "" self.worksheet = "" self.stats = "" self.stat_cells = "" self.__class__.all_players.append(self) def __str__(self): return self.name def get_stats(self, html): # gets stats each player html regex_string = self.name + self.regex reg_ex = re.compile(regex_string) search_object = reg_ex.search(html.string) if search_object not none: self.stats = search_object.groups() start, end = search_object.start(), search_object.end() html.string = html.string[:start] + html.string[end:] def print_report(self): print(self.name) print(self.stats) print(self.stat_cells) print(self.regex) print(self.worksheet) class html: """html single string of source code built urls given url text file. once built, html string may saved file , loaded (with advantage of speed , "bot" calls websites while testing). html string may searched, , once found search string may replaced.""" def __init__(self): self.string = "" def build(self, url_list): # build single string html sourcecode url in url_list: response = urllib.request.urlopen(url) self.string = self.string + str(response.read()) response.close() def load(self): file = open("state", 'rb') self.string = pickle.load(file) file.close() def save(self): file = open("state", 'wb') pickle.dump(self.string, file) file.close() def load_players(): # create players file, including spreadsheet data player_file = open(player_filename, 'r') line in player_file: line_list = line.rstrip().split('\t') if len(line_list) == 1: # check if line specifies worksheet or regex if line_list[0].startswith("regex"): regex = line[7:].rstrip() else: xl_worksheet = line_list[0] else: # line must player, create player object player_object = player(line_list[0]) player_object.worksheet = xl_worksheet player_object.regex = regex player_object.stat_cells = line_list[1:] def get_urls(): # build list of urls text file url_file = open(url_filename, 'r') url_list = [] each_line in url_file: url_list.append(each_line) url_file.close() return url_list def open_excel(): excel = dispatch('excel.application') excel.visible = true workbook = excel.workbooks.open(spreadsheet) return workbook def update_excel(player, workbook): count = 0 cell in player.stat_cells: col, row = cell_to_num(cell) sheet = workbook.sheets(player.worksheet) sheet.cells(row, col).value = (player.stats[count]) count = count + 1 def cell_to_num(cell): # convert excel cell notation ie b7 number ie 2, 7 col = "" split_cell = list(cell) in split_cell: if not i.isdigit(): col = col + split_cell = split_cell[1:] row = "".join(split_cell) col = col_to_num(col) return int(col), int(row) def col_to_num(col): # convert letter excel column ie or aa number if len(col) == 1: col = let_to_num[col.upper()] else: col = (let_to_num[col[0].upper()] * 26) + let_to_num[col[1].upper()] return col def set_spreadsheet(): global spreadsheet spreadsheet = input("please enter spreadsheet filename: ") spreadsheet = os.path.join(os.getcwd(), spreadsheet) def set_pfilename(): global player_filename player_filename = input("please enter player filename: ") def set_ufilename(): global url_filename url_filename = input("please enter url filename: ") def main(): load_players() url_list = get_urls() html = html() # html.build(url_list) html.load() workbook = open_excel() player in player.all_players: player.get_stats(html) update_excel(player, workbook) if __name__ == 'main': main()code:# dostats_gui.py # # "drives" dostats.py program # simple tkinter interface. import dostats tkinter import * tkinter import filedialog import os class app: def __init__(self, master): master.wm_title('do stats') master.wm_iconbitmap('icon.ico') frame = frame(master) frame.pack() self.sfile_label = label(frame, text="spreadsheet: ", width='14', anchor='e') self.sfile_label.grid(row=0, column=0) sfile = os.path.basename(dostats.spreadsheet) self.sfile_but = button(frame, text=sfile, command=lambda b='sht': self.set_filename(b)) self.sfile_but.grid(row=0, column=1) self.pfile_label = label(frame, text="players: ", width='14', anchor='e') self.pfile_label.grid(row=1, column=0) self.pfile_but = button(frame, text=dostats.player_filename, command=lambda b='plyr': self.set_filename(b)) self.pfile_but.grid(row=1, column=1) self.ufile_label = label(frame, text="urls: ", width='14', anchor='e') self.ufile_label.grid(row=2, column=0) self.ufile_but = button(frame, text=dostats.url_filename, command=lambda b='url': self.set_filename(b)) self.ufile_but.grid(row=2, column=1) self.stats_but = button(frame, text="do stats", command=dostats.main) self.stats_but.grid(row=3, column=1) def set_filename(self, b): filename = filedialog.askopenfilename() if b == "url": dostats.url_filename = filename self.ufile_but['text'] = os.path.basename(filename) elif b == "plyr": dostats.player_filename = filename self.pfile_but['text'] = os.path.basename(filename) else: dostats.spreadsheet = filename self.sfile_but['text'] = os.path.basename(filename) root = tk() app = app(root) root.mainloop()
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] How to make a Python script run on Windows?
Ubuntu
Comments
Post a Comment