Python (docx) excel 和 word

问题描述 投票:0回答:0

如何删除写入 Word 文件的表格的内部边框?

我在Word文件中创建表格,然后将excel文件中的信息写入其中,我希望Word中的表格没有内边框,我不知道该怎么做

审查了所有可能的内容,但仍然没有找到,他们写了很多 table.style = 'Table Grid 这会删除内部边界,但这不起作用

import tkinter as tk
from tkinter import filedialog
import openpyxl
import docx
from docx import Document
from docx.shared import Cm

# Create the main window
root = tk.Tk()
root.withdraw()

# Open a file dialog to select the Excel file
file_path = filedialog.askopenfilename(title="Select Excel file", filetypes=(("Excel files", "*.xlsx"), ("All files", "*.*")))

# Open the Excel file and select the worksheet
wb = openpyxl.load_workbook(file_path)
sheet = wb['Sheet1']

doc = Document()

section = doc.sections[0]

margin = docx.shared.Inches(0.1)
margin2 = docx.shared.Inches(0.5)
margin3 = docx.shared.Inches(0.3)

section.left_margin = margin3
section.right_margin = margin
section.top_margin = margin
section.bottom_margin = margin2

table_count = 0
row = None
for i in range(2, sheet.max_row + 1):
    if sheet[f'B{i}'].value:
        for j in range(int(sheet[f'F{i}'].value)):
            
            # Check if we need to start a new row
            if table_count % 2 == 0:
                row = doc.add_table(rows=1, cols=2).rows[0]
                row.height = Cm(3)
                row.cells[0].width = Cm(25)
                row.cells[1].width = Cm(25)
            
            # Add a table to the current row
            table = row.cells[table_count % 2].add_table(rows=7, cols=2)
            table.style = 'Table Grid'
            table.autofit = False

            # Remove internal borders from the table


            # Merge the first row
            table.cell(0, 0).merge(table.cell(0, 1))
            table.cell(6, 0).merge(table.cell(6, 1))

            # Modify the width of the first column
            for cell in table.columns[0].cells:
                cell.width = Cm(2.5)

            # Modify the width of the second column
            for cell in table.columns[1].cells:
                cell.width = Cm(5.5)
            
            nos = sheet[f"B{i}"].value if sheet[f"B{i}"].value else "N/A"
            nom_nr = sheet[f"J{i}"].value if sheet[f"J{i}"].value else "N/A"
            kods = sheet[f"D{i}"].value if sheet[f"D{i}"].value else "N/A"
            pas_nr = sheet[f"K{i}"].value if sheet[f"K{i}"].value else "N/A"

            table.cell(0, 0).text = "Nos.: "
            table.cell(0, 0).paragraphs[0].add_run(f"{nos}").bold = True
            table.cell(1, 0).text = "Nom. Nr.: "
            table.cell(1, 1).text = str(nom_nr)
            table.cell(1, 1).paragraphs[0].runs[0].bold = True  # make the variable bold
            table.cell(2, 0).text = "Kods: "
            table.cell(2, 1).text = str(kods)
            table.cell(2, 1).paragraphs[0].runs[0].bold = True  # make the variable bold
            table.cell(3, 0).text = "Pas. Nr.: "
            table.cell(3, 1).text = str(pas_nr)
            table.cell(3, 1).paragraphs[0].runs[0].bold = True  # make the variable bold
            table.cell(4, 0).text = "Daudzums: "
            table.cell(4, 1).text = "1000"
            table.cell(4, 1).paragraphs[0].runs[0].bold = True  # make the variable bold
            table.cell(5, 0).text = "   "
            table.cell(5, 1).text = "   "
            table.cell(6, 0).text = "Izpildītājs: SIA “STS Group”\nCeļāres 2B, Spilve, Babītes pag.\nTel. 29211780"
            
            # Increment the table count
            table_count += 1

# Save the Word document
doc.save('data.docx')
python openpyxl python-docx
© www.soinside.com 2019 - 2024. All rights reserved.