Пример #1
0
def wordless_msg_box_path_not_exist_confirm(main, path):
    reply = QMessageBox.question(
        main, main.tr('Path Not Exist'),
        main.tr(f'''
                                     {main.settings_global['styles']['style_dialog']}
                                     <body>
                                         <div>The specified path "{path}" does not exist.</div>
                                         <div>Do you want to create the directory?</div>
                                     </body>
                                 '''), QMessageBox.Yes | QMessageBox.No,
        QMessageBox.No)

    if reply == QMessageBox.Yes:
        wordless_checking_misc.check_dir(path)

    return reply
Пример #2
0
    def open_files(self):
        if os.path.exists(
                self.main.settings_custom['import']['files']['default_path']):
            default_dir = self.main.settings_custom['import']['files'][
                'default_path']
        else:
            default_dir = self.main.settings_default['import']['files'][
                'default_path']

        file_paths = QFileDialog.getOpenFileNames(
            self.main, self.tr('Open File(s)'),
            wordless_checking_misc.check_dir(default_dir),
            ';;'.join(self.main.settings_global['file_types']['files']),
            self.main.settings_global['file_types']['files'][-1])[0]

        if file_paths:
            self.main.wordless_files.add_files(file_paths)
Пример #3
0
    def export_list(self, settings):
        default_dir = self.main.settings_custom['export'][settings]['default_path']

        file_path = QFileDialog.getSaveFileName(self.main,
                                                self.tr('Export to File'),
                                                wordless_checking_misc.check_dir(default_dir),
                                                self.tr('Text File (*.txt)'))[0]

        if file_path:
            encoding = self.main.settings_custom['export'][settings]['default_encoding']

            with open(file_path, 'w', encoding = encoding) as f:
                for item in self.get_items():
                    f.write(item + '\n')

            wordless_msg_box.wordless_msg_box_export_list(self.main, file_path)

            self.main.settings_custom['export'][settings]['default_path'] = os.path.normpath(os.path.dirname(file_path))
Пример #4
0
    def export_all(self, rows_export=[]):
        def set_cell_styles(cell, item, item_type='item'):
            if item_type == 'header_horizontal':
                cell.font = openpyxl.styles.Font(name=item.font().family(),
                                                 size=8,
                                                 bold=True,
                                                 color='FFFFFF')

                if self.header_orientation == 'horizontal':
                    cell.fill = openpyxl.styles.PatternFill(fill_type='solid',
                                                            fgColor='5C88C5')
                else:
                    cell.fill = openpyxl.styles.PatternFill(fill_type='solid',
                                                            fgColor='888888')
            elif item_type == 'header_vertical':
                cell.font = openpyxl.styles.Font(name=item.font().family(),
                                                 size=8,
                                                 bold=True,
                                                 color='FFFFFF')

                cell.fill = openpyxl.styles.PatternFill(fill_type='solid',
                                                        fgColor='5C88C5')
            else:
                cell.font = openpyxl.styles.Font(name=item.font().family(),
                                                 size=8,
                                                 color='292929')

            cell.alignment = openpyxl.styles.Alignment(horizontal='center',
                                                       vertical='center',
                                                       wrap_text=True)

        default_dir = self.main.settings_custom['export']['tables'][
            'default_path']

        (file_path, file_type) = QFileDialog.getSaveFileName(
            self, self.tr('Export Table'),
            wordless_checking_misc.check_dir(default_dir), ';;'.join(
                self.main.settings_global['file_types']['export_tables']),
            self.main.settings_custom['export']['tables']['default_type'])

        if file_path:
            if file_type == self.tr('Excel Workbook (*.xlsx)'):
                workbook = openpyxl.Workbook()
                worksheet = workbook.active

                worksheet.freeze_panes = 'B2'

                dpi_horizontal = QApplication.primaryScreen(
                ).logicalDotsPerInchX()
                dpi_vertical = QApplication.primaryScreen(
                ).logicalDotsPerInchY()

                if not rows_export:
                    rows_export = list(range(self.rowCount()))

                if self.header_orientation == 'horizontal':
                    # Horizontal Headers
                    for col in range(self.columnCount()):
                        worksheet.cell(
                            1, 1 +
                            col).value = self.horizontalHeaderItem(col).text()

                        set_cell_styles(worksheet.cell(1, 1 + col),
                                        self.horizontalHeaderItem(col),
                                        item_type='header_horizontal')

                        worksheet.column_dimensions[
                            openpyxl.utils.get_column_letter(
                                1 + col)].width = self.horizontalHeader(
                                ).sectionSize(col) / dpi_horizontal * 13 + 3

                    # Cells
                    for row_cell, row_item in enumerate(rows_export):
                        for col in range(self.columnCount()):
                            worksheet.cell(2 + row_cell,
                                           1 + col).value = self.item(
                                               row_item, col).text()

                            set_cell_styles(
                                worksheet.cell(2 + row_cell, 1 + col),
                                self.item(row_item, col))
                else:
                    # Horizontal Headers
                    for col in range(self.columnCount()):
                        worksheet.cell(
                            1, 2 +
                            col).value = self.horizontalHeaderItem(col).text()

                        set_cell_styles(worksheet.cell(1, 2 + col),
                                        self.horizontalHeaderItem(col),
                                        item_type='header_horizontal')

                        worksheet.column_dimensions[
                            openpyxl.utils.get_column_letter(
                                2 + col)].width = self.horizontalHeader(
                                ).sectionSize(col) / dpi_horizontal * 13 + 3

                    worksheet.column_dimensions[
                        openpyxl.utils.get_column_letter(
                            1)].width = self.verticalHeader().width(
                            ) / dpi_horizontal * 13 + 3

                    # Vertical Headers
                    for row_cell, row_item in enumerate(rows_export):
                        worksheet.cell(2 + row_cell,
                                       1).value = self.verticalHeaderItem(
                                           row_item).text()

                        set_cell_styles(worksheet.cell(2 + row_cell, 1),
                                        self.verticalHeaderItem(row_item),
                                        item_type='header_vertical')

                    # Cells
                    for row_cell, row_item in enumerate(rows_export):
                        for col in range(self.columnCount()):
                            worksheet.cell(2 + row_cell,
                                           2 + col).value = self.item(
                                               row_item, col).text()

                            set_cell_styles(
                                worksheet.cell(2 + row_cell, 2 + col),
                                self.item(row_item, col))

                # Row Height
                worksheet.row_dimensions[1].height = self.horizontalHeader(
                ).height() / dpi_vertical * 72

                for i, _ in enumerate(worksheet.rows):
                    worksheet.row_dimensions[
                        2 + i].height = self.verticalHeader().sectionSize(
                            0) / dpi_vertical * 72

                # Borders
                border = openpyxl.styles.Side(border_style='thin',
                                              color='292929')

                for row, _ in enumerate(worksheet.rows):
                    for col, _ in enumerate(worksheet.columns):
                        worksheet.cell(row + 1, col +
                                       1).border = openpyxl.styles.Border(
                                           left=border,
                                           right=border,
                                           top=border,
                                           bottom=border)

                workbook.save(file_path)
            elif file_type == self.tr('CSV File (*.csv)'):
                encoding = self.main.settings_custom['export']['tables'][
                    'default_encoding']

                with open(file_path, 'w', encoding=encoding, newline='') as f:
                    csv_writer = csv.writer(f)

                    if not rows_export:
                        rows_export = list(range(self.rowCount()))

                    if self.header_orientation == 'horizontal':
                        # Horizontal Headers
                        csv_writer.writerow([
                            self.horizontalHeaderItem(col).text().strip()
                            for col in range(self.columnCount())
                        ])

                        # Cells
                        for row in rows_export:
                            csv_writer.writerow([
                                self.item(row, col).text().strip()
                                for col in range(self.columnCount())
                            ])

                    else:
                        # Horizontal Headers
                        csv_writer.writerow([''] + [
                            self.horizontalHeaderItem(col).text().strip()
                            for col in range(self.columnCount())
                        ])

                        # Vertical Headers & Cells
                        for row in rows_export:
                            csv_writer.writerow(
                                [self.verticalHeaderItem(row).text().strip()] +
                                [
                                    self.item(row, col).text().strip()
                                    for col in range(self.columnCount())
                                ])

            self.main.settings_custom['export']['tables'][
                'default_path'] = os.path.normpath(os.path.dirname(file_path))
            self.main.settings_custom['export']['tables'][
                'default_type'] = file_type

            wordless_msg_box.wordless_msg_box_export_table(
                self.main, file_path)
Пример #5
0
    def add_files(self):
        new_files = []

        files_detection_failed_encoding = []
        files_detection_failed_text_type = []
        files_detection_failed_lang = []

        if self.file_paths:
            len_file_paths = len(self.file_paths)

            for i, file_path in enumerate(self.file_paths):
                self.progress_updated.emit(
                    self.tr(f'Loading files ... ({i + 1}/{len_file_paths})'))

                default_dir = wordless_checking_misc.check_dir(
                    self.main.settings_custom['import']['temp_files']
                    ['default_path'])
                default_encoding = self.main.settings_custom['import'][
                    'temp_files']['default_encoding']

                file_path = wordless_misc.get_abs_path(file_path)
                file_name, file_ext = os.path.splitext(
                    os.path.basename(file_path))
                file_ext = file_ext.lower()

                # Text Files
                if file_ext == '.txt':
                    (new_file, detection_success_encoding,
                     detection_success_text_type, detection_success_lang
                     ) = self.main.wordless_files._new_file(file_path)

                    new_files.append(new_file)

                    if not detection_success_encoding:
                        files_detection_failed_encoding.append(
                            new_file['path'])

                    if not detection_success_text_type:
                        files_detection_failed_text_type.append(
                            new_file['path'])

                    if not detection_success_lang:
                        files_detection_failed_lang.append(new_file['path'])
                else:
                    if file_ext in ['.docx', '.xlsx', '.xls']:
                        new_path = wordless_checking_misc.check_new_path(
                            os.path.join(default_dir, f'{file_name}.txt'))

                        # Word Documents
                        if file_ext == '.docx':
                            lines = []

                            with open(new_path, 'w',
                                      encoding=default_encoding) as f:
                                doc = docx.Document(file_path)

                                for block in self.iter_block_items(doc):
                                    if type(block
                                            ) == docx.text.paragraph.Paragraph:
                                        f.write(f'{block.text}\n')
                                    elif type(block) == docx.table.Table:
                                        for row in self.iter_visual_cells(
                                                block):
                                            cells = []

                                            for cell in row:
                                                cells.append(' '.join([
                                                    item.text for item in
                                                    self.iter_cell_items(cell)
                                                ]))

                                            f.write('\t'.join(cells) + '\n')

                        # Excel Workbooks
                        elif file_ext == '.xlsx':
                            with open(new_path, 'w',
                                      encoding=default_encoding) as f:
                                workbook = openpyxl.load_workbook(
                                    file_path, data_only=True)

                                for worksheet_name in workbook.sheetnames:
                                    worksheet = workbook[worksheet_name]

                                    for row in worksheet.rows:
                                        f.write('\t'.join([(
                                            cell.value if cell.value != None
                                            else '') for cell in row]) + '\n')
                        elif file_ext == '.xls':
                            with open(new_path, 'w',
                                      encoding=default_encoding) as f:
                                workbook = xlrd.open_workbook(file_path)

                                for i_sheet in range(workbook.nsheets):
                                    worksheet = workbook.sheet_by_index(
                                        i_sheet)

                                    for row in range(worksheet.nrows):
                                        f.write('\t'.join([
                                            worksheet.cell_value(row, col)
                                            for col in range(worksheet.ncols)
                                        ]) + '\n')

                        new_paths = [new_path]
                    else:
                        # Detect encoding
                        if self.main.settings_custom['files'][
                                'auto_detection_settings']['detect_encodings']:
                            encoding_code, _ = wordless_detection.detect_encoding(
                                self.main, file_path)
                        else:
                            encoding_code = self.main.settings_custom[
                                'encoding_detection']['default_settings'][
                                    'default_encoding']

                        # CSV Files
                        if file_ext == '.csv':
                            new_path = wordless_checking_misc.check_new_path(
                                os.path.join(default_dir, f'{file_name}.txt'))

                            with open(new_path, 'w',
                                      encoding=default_encoding) as f:
                                with open(file_path,
                                          'r',
                                          newline='',
                                          encoding=encoding_code) as f_csv:
                                    csv_reader = csv.reader(f_csv)

                                    for row in csv_reader:
                                        f.write('\t'.join(row) + '\n')

                            new_paths = [new_path]

                        # HTML Files
                        elif file_ext in ['.htm', '.html']:
                            with open(file_path, 'r',
                                      encoding=encoding_code) as f:
                                soup = bs4.BeautifulSoup(f.read(), 'lxml')

                            new_path = wordless_checking_misc.check_new_path(
                                os.path.join(default_dir, f'{file_name}.txt'))

                            with open(new_path, 'w',
                                      encoding=default_encoding) as f:
                                f.write(soup.get_text())

                            new_paths = [new_path]

                        # Translation Memory Files
                        elif file_ext == '.tmx':
                            lines_src = []
                            lines_target = []

                            with open(file_path, 'r',
                                      encoding=encoding_code) as f:
                                soup = bs4.BeautifulSoup(f.read(), 'lxml-xml')

                                for tu in soup.find_all('tu'):
                                    seg_src, seg_target = tu.find_all('seg')

                                    lines_src.append(seg_src.get_text())
                                    lines_target.append(seg_target.get_text())

                            path_src = wordless_checking_misc.check_new_path(
                                os.path.join(default_dir,
                                             f'{file_name}_source.txt'))
                            path_target = wordless_checking_misc.check_new_path(
                                os.path.join(default_dir,
                                             f'{file_name}_target.txt'))

                            with open(path_src, 'w',
                                      encoding=default_encoding) as f:
                                f.write('\n'.join(lines_src))
                                f.write('\n')

                            with open(path_target,
                                      'w',
                                      encoding=default_encoding) as f:
                                f.write('\n'.join(lines_target))
                                f.write('\n')

                            new_paths = [path_src, path_target]

                        # Lyrics Files
                        elif file_ext == '.lrc':
                            lyrics = {}

                            with open(file_path, 'r',
                                      encoding=encoding_code) as f:
                                for line in f:
                                    time_tags = []

                                    line = line.strip()

                                    # Strip time tags
                                    while re.search(r'^\[[^\]]+?\]', line):
                                        time_tags.append(
                                            re.search(r'^\[[^\]]+?\]',
                                                      line).group())

                                        line = line[len(time_tags[-1]):].strip(
                                        )

                                    # Strip word time tags
                                    line = re.sub(r'<[^>]+?>', r'', line)
                                    line = re.sub(r'\s{2,}', r' ',
                                                  line).strip()

                                    for time_tag in time_tags:
                                        if re.search(
                                                r'^\[[0-9]{2}:[0-5][0-9]\.[0-9]{2}\]$',
                                                time_tag):
                                            lyrics[time_tag] = line

                            new_path = wordless_checking_misc.check_new_path(
                                os.path.join(default_dir, f'{file_name}.txt'))

                            with open(new_path, 'w',
                                      encoding=default_encoding) as f:
                                for _, lyrics in sorted(lyrics.items()):
                                    f.write(f'{lyrics}\n')

                            new_paths = [new_path]

                    for new_path in new_paths:
                        (new_file, detection_success_encoding,
                         detection_success_text_type, detection_success_lang
                         ) = self.main.wordless_files._new_file(new_path)

                        new_files.append(new_file)

                        if not detection_success_encoding:
                            files_detection_failed_encoding.append(
                                new_file['path'])

                        if not detection_success_text_type:
                            files_detection_failed_text_type.append(
                                new_file['path'])

                        if not detection_success_lang:
                            files_detection_failed_lang.append(
                                new_file['path'])

            self.main.settings_custom['import']['files'][
                'default_path'] = wordless_misc.get_abs_path(
                    os.path.dirname(self.file_paths[0]))

        self.files_added.emit(new_files, files_detection_failed_encoding,
                              files_detection_failed_text_type,
                              files_detection_failed_lang)