import xlwings as xw
import time
from PIL import ImageGrab
def create_excel():
try:
answer = input('是否处理当前example.xlsx文件,y/n?\n')
if answer == 'y' or answer == 'Y':
path = 'example.xlsx'
else:
path = input('请输入当前文件中需要进行处理的文件名,如:员工信息.xlsx\n')
if path == '':
return
# wb = xw.Book(f'{path}')
app = xw.App(visible=True, add_book=False)
wb = app.books.open(f'{path}')
# wb = load_workbook(path, data_only=True)
# sh = wb.active
print('默认处理第一个工作表')
sh = wb.sheets[0]
# example.xlsx
title = sh.range('a1:o2').value
for row in sh.range('a3').expand().value:
data = []
data.append(title)
data.append(row)
print(f'{row[0]}数据处理中...')
# tmp_wb = xw.Book()
tmp_wb = app.books.add()
tmp_sh = tmp_wb.sheets[0]
tmp_sh.range('a1').value = title
tmp_sh.range('a1:o1').merge()
tmp_sh.range('a1').api.HorizontalAlignment = -4108
tmp_sh.range('a1').api.VerticalAlignment = -4130
tmp_sh.range('a1').autofit()
tmp_sh.range('a1').color = (111, 111, 255)
tmp_sh.range('a3').value = row
for r in tmp_sh.range('a2').expand('table').rows:
for cell in r:
cell.color = (111, 111, 255)
# cell.horizontal_alignment = 'center'
# cell.vertical_alignment = 'center'
cell.api.HorizontalAlignment = -4108
cell.api.VerticalAlignment = -4130
cell.api.Borders(9).LineStyle = 1
cell.api.Borders(9).Weight = 2
cell.api.Borders(7).LineStyle = 1
cell.api.Borders(7).Weight = 2
cell.api.Borders(8).LineStyle = 1
cell.api.Borders(8).Weight = 2
cell.api.Borders(10).LineStyle = 1
cell.api.Borders(10).Weight = 2
tmp_sh.autofit()
# print(tmp_sh.range('c2').value)
# print(f"{tmp_sh.range('a3').value}数据处理中...")
tmp_wb.save(f"{tmp_sh.range('a3').value}.xlsx")
all = tmp_sh.used_range
all.api.CopyPicture()
tmp_sh.api.Paste()
pic = tmp_sh.pictures[-1]
pic.api.Copy()
time.sleep(3)
img = ImageGrab.grabclipboard()
img.save(f"{tmp_sh.range('a3').value}.png")
tmp_wb.close()
wb.close()
input('已处理完成!按任意键退出!!')
except Exception as err:
tmp_wb.close()
input(f'出错了,请关闭所有文件后重试!!\n{err}\n')
wb.close()
app.kill()
wb.close()
app.kill()
create_excel()
Python实操自动生成单独的个人信息文件及图片文件
最新推荐文章于 2024-06-17 17:35:06 发布