コード例 #1
0
ファイル: ncGUI.py プロジェクト: edwardyliu/Scripts
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        credit_title = tk.Label(self, text="Credits Page", font=LARGE_FONT)
        credit_title.configure(background='#ffffff')
        credit_title.pack(pady=5, padx=10)

        author_label = tk.Label(self,
                                text="by: Edward Yifeng Liu",
                                font=LARGE_FONT)
        author_label.configure(background='#ffffff')
        author_label.pack(pady=32, padx=16)

        # Return to Main Window
        return_photo = tk.PhotoImage(
            file=ncUtils.resource_path("image/icons8-home-page-60.png"))
        return_url = tk.Button(
            self,
            image=return_photo,
            width=60,
            height=60,
            command=lambda: controller.show_frame(ProgramWindow))
        return_url.image = return_photo
        return_url.configure(background="#ffffff")
        return_url.pack(side="bottom", fill=tk.X)
コード例 #2
0
def main(argv, logs=True):
    
    print("Generating ...")
    
    # configurations
    print("  #1. Configuring ...")

    config = configparser.ConfigParser()
    config.read(ncUtils.resource_path('config.ini'))

    # populate all necessary mapping params
    outputPath = ncUtils.resource_path(config['DEFAULT']['outputPath'])
    cachePath = ncUtils.resource_path(config['DEFAULT']['cachePath'])

    # mkdir if not exists
    if not os.path.exists(outputPath):
        print("\ta) Building Directory: {}".format(outputPath))
        os.makedirs(outputPath)
    if not os.path.exists(cachePath):
        print("\tb) Building Directory: {}".format(cachePath))
        os.makedirs(cachePath)

    docx_filepaths = ncUtils.str_split_then_strip(config['DOCX']['filepaths'])
    docx_placeholders = ncUtils.str_split_then_strip(config['DOCX']['placeholders'])
    xlsx_filepaths = ncUtils.str_split_then_strip(config['XLSX']['filepaths'])
    xlsx_sheets = ncUtils.str_split_then_strip(config['XLSX']['sheets'])
    xlsx_columns = ncUtils.str_split_then_strip(config['XLSX']['column_names'])

    print("  #2. Processing ... ")
    if logs:
        bar = progressbar.ProgressBar(maxval=len(docx_filepaths), 
            widgets=['\t\t\t',progressbar.Bar('=', '[', ']', ), ' ', progressbar.Percentage()])
        bar.start()
    
    for idx, docx_filepath in enumerate(docx_filepaths):
        
        xlsx_column = ncUtils.xlsx_fetch_column_aslist(xlsx_filepaths[idx], xlsx_sheets[idx], xlsx_columns[idx])
        generate(docx_filepath, docx_placeholders[idx], xlsx_column, outputPath, cachePath, docx_filepath[docx_filepath.rfind('/')+1:-5], fill="__________________")

        if logs:
            sleep(0.5)
            print("      #{} {}".format(idx+1, docx_filepath[-14:]))
            bar.update(idx+1)
    if logs:
        bar.finish()
    print("Generation Complete.")
コード例 #3
0
def generate(template_filepath, placeholder, xlsx_mapping, \
    outputPath=ncUtils.resource_path("output/"), cachePath=ncUtils.resource_path("output/tmp/"), cache_basefilename="tmp", sep="-", fill=" "):

    template = Document(template_filepath)
    partition_no = 1

    for replacement in xlsx_mapping: # for each replacement
        while (ncUtils.docx_replace_one_tablecell(template, placeholder, replacement) == False): # replace 'placeholder' until there is nothing to be replaced
            # then, save this temporary docx as a component of the final docx
            template.save(cachePath+cache_basefilename+sep+str(partition_no)+".docx")
            template = Document(template_filepath)

            partition_no += 1
    
    # replace the remaining 'placeholder's with $fill, and save
    while (ncUtils.docx_replace_one_tablecell(template, placeholder, fill) == True):
        continue
    template.save(cachePath+cache_basefilename+sep+str(partition_no)+".docx")

    # generate filepaths to docx partitions
    partition_filepaths = ncUtils.docx_generate_filepaths(cachePath+cache_basefilename+sep, partition_no)
    
    # merge saved components and save as final docx
    ncUtils.docx_merge_then_save(partition_filepaths, outputPath+cache_basefilename+sep+"fin.docx")
コード例 #4
0
ファイル: ncGUI.py プロジェクト: edwardyliu/Scripts
    def tv_config(self):
        if not self.validated:
            self.logs_panel.insert(tk.END, "Please pass validation!\n")
            self.logs_panel.see(tk.END)
        else:
            config = configparser.ConfigParser()
            config['DEFAULT'] = {
                'outputPath': 'output/',
                'cachePath': 'output/tmp/'
            }
            config['DOCX'] = {}
            config['XLSX'] = {}

            docx_filepaths = ''
            docx_placeholders = ''
            xlsx_filepaths = ''
            xlsx_sheets = ''
            xlsx_colnames = ''
            for datarow in self.treeview.get_children():
                for entry in self.treeview.get_children(datarow):
                    if self.__iids[0] in entry:
                        docx_filepaths += self.treeview.item(entry, 'text')
                    elif self.__iids[1] in entry:
                        docx_placeholders += self.treeview.item(entry, 'text')
                    elif self.__iids[2] in entry:
                        xlsx_filepaths += self.treeview.item(entry, 'text')
                    elif self.__iids[3] in entry:
                        xlsx_sheets += self.treeview.item(entry, 'text')
                    elif self.__iids[4] in entry:
                        xlsx_colnames += self.treeview.item(entry, 'text')

                if not datarow in self.treeview.get_children()[-1]:
                    docx_filepaths += ','
                    docx_placeholders += ','
                    xlsx_filepaths += ','
                    xlsx_sheets += ','
                    xlsx_colnames += ','

            config['DOCX']['filepaths'] = docx_filepaths
            config['DOCX']['placeholders'] = docx_placeholders
            config['XLSX']['filepaths'] = xlsx_filepaths
            config['XLSX']['sheets'] = xlsx_sheets
            config['XLSX']['column_names'] = xlsx_colnames

            with open(ncUtils.resource_path('config.ini'), 'w') as configfile:
                config.write(configfile)
            self.logs_panel.insert(tk.END, "New config file synthesized.\n")
            self.logs_panel.see(tk.END)
コード例 #5
0
ファイル: ncGUI.py プロジェクト: edwardyliu/Scripts
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("Name Card Lynker [GUI Edition]  |  by Edward Y. Liu")
        self.resizable(0, 0)
        self.iconbitmap(ncUtils.resource_path("image/icons8-linking-50.ico"))

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for frame in (ProgramWindow, ReadmeWindow, CreditWindow):
            F = frame(container, self)
            F.configure(background="#ffffff")
            self.frames[frame] = F
            F.grid(row=0, column=0, sticky="nsew")

        self.show_frame(ProgramWindow)
コード例 #6
0
ファイル: ncGUI.py プロジェクト: edwardyliu/Scripts
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        readme_title = tk.Label(self, text="ReadMe Page", font=LARGE_FONT)
        readme_title.configure(background='#ffffff')
        readme_title.grid(row=0, column=0, columnspan=6, sticky="new")

        readme_text = tk.Text(self)
        readme_text.configure(font=TINY_FONT)
        readme_text.insert(tk.END, "a) Add Mapping: \n")
        readme_text.insert(
            tk.END,
            "   #1. Select DOCX File using 'Find DOCX'\n   #2. Fill in $Placeholder value\n   #3. Select XLSX File using 'Find XLSX'\n   #4. Fill in $Sheet Name\n   #5. Fill in $Column Header Name\n   #6. Click add button.\n\n"
        )
        readme_text.insert(tk.END, "b) Validate: Click validate button.\n")
        readme_text.insert(tk.END, "c) Configure: Click configure button.\n")
        readme_text.insert(tk.END, "d) Generate: Click generate button.\n\n\n")
        readme_text.insert(
            tk.END,
            "By default: files are pipped into the './output/' folder.")

        readme_text.grid(row=1, column=0, columnspan=6, sticky="nsew")

        # Return to Main Window
        return_photo = tk.PhotoImage(
            file=ncUtils.resource_path("image/icons8-home-page-60.png"))
        return_url = tk.Button(
            self,
            image=return_photo,
            width=60,
            height=60,
            command=lambda: controller.show_frame(ProgramWindow))
        return_url.image = return_photo
        return_url.configure(background="#ffffff")
        return_url.grid(row=2, column=0, columnspan=6, sticky="sew")

        self.columnconfigure(1, weight=1)
        self.rowconfigure(1, weight=1)
コード例 #7
0
ファイル: ncGUI.py プロジェクト: edwardyliu/Scripts
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # IID Icons
        self.__iid_icons.append(
            tk.PhotoImage(file=ncUtils.resource_path(
                "image/icons8-microsoft-word-16.png")))
        self.__iid_icons.append(
            tk.PhotoImage(file=ncUtils.resource_path(
                "image/icons8-placeholder-thumbnail-xml-16.png")))
        self.__iid_icons.append(
            tk.PhotoImage(
                file=ncUtils.resource_path("image/icons8-xls-16.png")))
        self.__iid_icons.append(
            tk.PhotoImage(
                file=ncUtils.resource_path("image/icons8-sheet-metal-16.png")))
        self.__iid_icons.append(
            tk.PhotoImage(
                file=ncUtils.resource_path("image/icons8-add-column-16.png")))

        # Title
        menu_title_photo = tk.PhotoImage(
            file=ncUtils.resource_path("image/title.png"))
        self.menu_title = tk.Label(self, image=menu_title_photo)
        self.menu_title.image = menu_title_photo
        self.menu_title.configure(background="#ffffff")
        self.menu_title.grid(row=0, column=0, columnspan=6, sticky="nsew")

        # README
        readme_photo = tk.PhotoImage(
            file=ncUtils.resource_path("image/icons8-readme-32.png"))
        self.readme_url = tk.Button(
            self,
            image=readme_photo,
            height=32,
            width=32,
            command=lambda: controller.show_frame(ReadmeWindow))
        self.readme_url.image = readme_photo
        self.readme_url.configure(background="#ffffff")
        self.readme_url.grid(row=1,
                             column=0,
                             columnspan=1,
                             padx=5,
                             pady=5,
                             sticky="nsew")

        # Credit
        credit_photo = tk.PhotoImage(
            file=ncUtils.resource_path("image/icons8-people-32.png"))
        self.credit_url = tk.Button(
            self,
            image=credit_photo,
            height=32,
            width=32,
            command=lambda: controller.show_frame(CreditWindow))
        self.credit_url.image = credit_photo
        self.credit_url.configure(background="#ffffff")
        self.credit_url.grid(row=1,
                             column=1,
                             columnspan=1,
                             padx=5,
                             pady=5,
                             sticky="nsew")

        # Input Fields
        # DOCX
        self.docx_entry = tk.Entry(self)
        self.docx_entry.delete(0, tk.END)
        self.docx_entry.insert(0, "docx-path")
        self.docx_entry.grid(row=3,
                             column=0,
                             columnspan=1,
                             padx=5,
                             pady=5,
                             sticky="nsew")
        self.docx_button = tk.Button(
            self,
            text="Find DOCX",
            font=SMALL_FONT,
            command=lambda: controller.set_tkentry(
                self.docx_entry, tk.filedialog.askopenfilename()))
        self.docx_button.grid(row=2,
                              column=0,
                              columnspan=1,
                              padx=5,
                              pady=5,
                              sticky="nsew")

        # Placeholder Text
        self.plchdr_entry = tk.Entry(self)
        self.plchdr_entry.delete(0, tk.END)
        self.plchdr_entry.insert(0, "«Name»")
        self.plchdr_entry.grid(row=3,
                               column=1,
                               columnspan=1,
                               padx=5,
                               pady=5,
                               sticky="nsew")
        self.plchdr_title = tk.Label(self, text="Placeholder", font=SMALL_FONT)
        self.plchdr_title.configure(background="#ffffff")
        self.plchdr_title.grid(row=2,
                               column=1,
                               columnspan=1,
                               padx=5,
                               pady=5,
                               sticky="nsew")

        # XLSX
        self.xlsx_entry = tk.Entry(self)
        self.xlsx_entry.delete(0, tk.END)
        self.xlsx_entry.insert(0, "xlsx-path")
        self.xlsx_entry.grid(row=3,
                             column=2,
                             columnspan=1,
                             padx=5,
                             pady=5,
                             sticky="nsew")
        self.xlsx_button = tk.Button(
            self,
            text="Find XLSX",
            font=SMALL_FONT,
            command=lambda: controller.set_tkentry(
                self.xlsx_entry, tk.filedialog.askopenfilename()))
        self.xlsx_button.grid(row=2,
                              column=2,
                              columnspan=1,
                              padx=5,
                              pady=5,
                              sticky="nsew")

        # Sheet Name
        self.sheet_entry = tk.Entry(self)
        self.sheet_entry.delete(0, tk.END)
        self.sheet_entry.insert(0, "Sheet1")
        self.sheet_entry.grid(row=3,
                              column=3,
                              columnspan=1,
                              padx=5,
                              pady=5,
                              sticky="nsew")
        self.sheet_title = tk.Label(self, text="Sheet Name", font=SMALL_FONT)
        self.sheet_title.configure(background="#ffffff")
        self.sheet_title.grid(row=2,
                              column=3,
                              columnspan=1,
                              padx=5,
                              pady=5,
                              sticky="nsew")

        # Column Name
        self.colname_entry = tk.Entry(self)
        self.colname_entry.delete(0, tk.END)
        self.colname_entry.insert(0, "Name")
        self.colname_entry.grid(row=3,
                                column=4,
                                columnspan=1,
                                padx=5,
                                pady=5,
                                sticky="nsew")
        self.colname_title = tk.Label(self,
                                      text="Column Header",
                                      font=SMALL_FONT)
        self.colname_title.configure(background="#ffffff")
        self.colname_title.grid(row=2,
                                column=4,
                                columnspan=1,
                                padx=5,
                                pady=5,
                                sticky="nsew")

        # TK Entries
        self.__tk_entries.append(self.docx_entry)
        self.__tk_entries.append(self.plchdr_entry)
        self.__tk_entries.append(self.xlsx_entry)
        self.__tk_entries.append(self.sheet_entry)
        self.__tk_entries.append(self.colname_entry)

        # List of Mapping Entries
        self.treeview = tk.ttk.Treeview(self)
        self.treeview.heading('#0', text="DOCX-XLSX Map View")
        self.treeview.bind("<3>", self.rhtclick_menu)
        self.treeview.bind("<Double-Button-1>", self.dbclick_menu)
        self.treeview.grid(row=4,
                           column=0,
                           columnspan=5,
                           padx=5,
                           pady=5,
                           sticky="nsew")

        # treeview scrollbar
        self.treeview_scroll = tk.Scrollbar(self)
        self.treeview_scroll.grid(row=4, column=5, sticky='nse')
        self.treeview_scroll.configure(command=self.treeview.yview)
        self.treeview.configure(yscrollcommand=self.treeview_scroll.set)

        # Add
        add_entry_photo = tk.PhotoImage(
            file=ncUtils.resource_path("image/icons8-add-32.png"))
        self.add_entry_button = tk.Button(self,
                                          image=add_entry_photo,
                                          width=32,
                                          height=32,
                                          command=lambda: self.tv_append())
        self.add_entry_button.image = add_entry_photo
        self.add_entry_button.configure(background="#ffffff")
        self.add_entry_button.grid(row=1,
                                   column=4,
                                   columnspan=1,
                                   padx=5,
                                   pady=5,
                                   sticky="nsew")

        self.columnconfigure(1, weight=1)
        self.rowconfigure(1, weight=1)

        # Validate
        # 1. check if file exist
        # 2. check if actually docx & xlsx
        self.validated = False
        self.validate_button = tk.Button(self,
                                         text="Validate",
                                         font=REGULAR_FONT,
                                         command=lambda: self.tv_validate())
        self.validate_button.grid(row=5,
                                  column=0,
                                  columnspan=1,
                                  padx=5,
                                  pady=5,
                                  sticky="nsew")

        # Logs
        self.logs_panel = tk.Text(self, height=7)
        self.logs_panel.configure(font=TINY_FONT)
        self.logs_panel.grid(row=6,
                             column=0,
                             columnspan=5,
                             padx=5,
                             pady=5,
                             sticky="nsew")

        # redirect stdout and stderr
        sys.stdout = TextRedirector(self.logs_panel, "stdout")
        sys.stderr = TextRedirector(self.logs_panel, "stderr")

        # logs scrollbar
        self.logs_scroll = tk.Scrollbar(self)
        self.logs_scroll.grid(row=6, column=5, sticky='nse')
        self.logs_scroll.configure(command=self.logs_panel.yview)
        self.logs_panel.configure(yscrollcommand=self.logs_scroll.set)

        # Config
        self.config_button = tk.Button(self,
                                       text='Configure',
                                       font=REGULAR_FONT,
                                       command=lambda: self.tv_config())
        self.config_button.grid(row=5,
                                column=1,
                                columnspan=1,
                                padx=5,
                                pady=5,
                                sticky="nsew")

        # Generate
        self.generate_button = tk.Button(self,
                                         text='Generate',
                                         font=REGULAR_FONT,
                                         command=lambda: self.generate())
        self.generate_button.grid(row=5,
                                  column=4,
                                  columnspan=1,
                                  padx=5,
                                  pady=5,
                                  sticky="nsew")