import tkinter as tk
import xlrd
from docx import Document
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.shared import Pt
from os import system
from docx.oxml.ns import qn
from tkinter import filedialog, messagebox
'''打开选择文件夹对话框'''
window = tk.Tk()
window.title("规划坐标合并1.0 cby: 放放风")
window.geometry('790x465+500+200')
window['background'] = 'Pink'
xlspath = filedialog.askopenfilenames(filetypes=[("文本", "xls")], title="请选择需要合并的规划文件")
Folderpath = filedialog.askopenfilenames(filetypes=[("文本", "docx")], title="请选择需要合并的规划文件")
openname = []
for ui in xlspath:
wb = xlrd.open_workbook(ui)
ws = wb.sheet_by_name("瓶窑镇乱占耕地建房整体情况分析表")
for n in range(1,ws.nrows):
name = str(ws.cell_value(n, 0))
xuhao = str(ws.cell_value(n,0))[1:]
weiyima = ws.cell_value(n,1)
ctype = type(ws.cell_value(n,1))
if isinstance(weiyima,float):
weiyima = str(int(weiyima))
print(type(weiyima))
cun = str( ws.cell_value( n, 2 ) )
bnryy = str(ws.cell_value(n,7))
val = str(ws.cell_value(n, 3))
oo = len(val)
for path in Folderpath:
path_names = path.split("/")
path_name = path_names[-1][:-5]
aname = name[1:]
if aname == path_name:
doc = Document(path)
tabs = doc.tables
table = tabs[0]
table.cell(1, 3).text = ""
run = table.cell(1, 3).paragraphs[0].add_run(val)
table.cell( 1, 0 ).text = ""
run2 = table.cell(1, 0).paragraphs[0].add_run(xuhao)
run2.font.name = "仿宋_GB2312"
run2._element.rPr.rFonts.set( qn( 'w:eastAsia' ), u'仿宋_GB2312' )
run2.font.size = Pt( 10 )
table.cell( 1, 0 ).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER
table.cell( 1, 1 ).text = ""
run3 = table.cell(1, 1).paragraphs[0].add_run(weiyima)
run3.font.name = "仿宋_GB2312"
run3._element.rPr.rFonts.set( qn( 'w:eastAsia' ), u'仿宋_GB2312' )
run3.font.size = Pt( 10 )
table.cell( 1, 1 ).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER
table.cell( 1, 2 ).text = ""
run4 = table.cell(1, 2).paragraphs[0].add_run(cun)
run4.font.name = "仿宋_GB2312"
run4._element.rPr.rFonts.set( qn( 'w:eastAsia' ), u'仿宋_GB2312' )
run4.font.size = Pt( 10 )
table.cell( 1, 2 ).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER
table.cell( 1, 8 ).text = ""
run5 = table.cell(1, 8).paragraphs[0].add_run(bnryy)
run5.font.name = "仿宋_GB2312"
run5._element.rPr.rFonts.set( qn( 'w:eastAsia' ), u'仿宋_GB2312' )
run5.font.size = Pt( 10 )
table.cell( 1, 8 ).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER
if oo <20:
run.font.name = "仿宋_GB2312"
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
run.font.size = Pt(9)
elif oo < 24:
run.font.name = "仿宋_GB2312"
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
run.font.size = Pt(7)
elif oo < 28:
run.font.name = "仿宋_GB2312"
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
run.font.size = Pt(6.5)
else:
run.font.name = "仿宋_GB2312"
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
run.font.size = Pt(5.5)
openname.append(path)
print( aname, path_name )
table.cell(1, 3).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER
doc.save(path)
for path23 in openname:
system( "start %s" % path23 )
tk.messagebox.showinfo('结果', "成功t")
window.mainloop()