main.py
import pandas as pd
from lib.func import copy_and_rename_file, modify_cell_value
def read_income_data(file_path):
df = pd.read_excel(file_path, sheet_name="收入明細(xì)", skiprows=4, index_col=0, dtype=str)
# df = df[:-1] # 去除最后一行數(shù)據(jù)
row_lists = df.values.tolist()
return row_lists
# 請將以下路徑替換為您的實(shí)際文件路徑
file_path = "倆表對比.xls"
data_lists = read_income_data(file_path)
print(data_lists)
for row_list in data_lists:
source_file = "模板.xlsx"
destination_folder = "生成"
new_file_name = f"{row_list[0]}.xlsx"
copy_and_rename_file(source_file, destination_folder, new_file_name)
file_path = f"生成/{row_list[0]}.xlsx"
sheet_name = "人均收入測算表"
cell_reference = "A2"
new_value = f"戶主: {row_list[0]}\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000戶類型:脫貧戶□ 防貧監(jiān)測戶□\n \n當(dāng)前家庭人口數(shù): {row_list[1]}\u3000\u3000\u3000年度家庭人口數(shù) {row_list[1]} \u3000\u3000\u3000\u3000\u3000\u3000調(diào)查時(shí)間:2024年\u3000\u3000月\u3000\u3000日"
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 公益崗
cell_reference = "B5"
new_value = row_list[18]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 最低生活保障金
cell_reference = "B15"
new_value = row_list[3]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 特困人員救助供養(yǎng)金
cell_reference = "B16"
new_value = row_list[4]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 養(yǎng)老金或離退休金
cell_reference = "B17"
new_value = row_list[5]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 計(jì)劃生育金
cell_reference = "B22"
new_value = row_list[6]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 生態(tài)補(bǔ)償金
cell_reference = "B23"
new_value = row_list[7]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 產(chǎn)業(yè)獎(jiǎng)補(bǔ)
cell_reference = "B24"
new_value = row_list[10]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 交通補(bǔ)助
cell_reference = "B25"
new_value = row_list[11]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 孝心養(yǎng)老獎(jiǎng)補(bǔ)
cell_reference = "B27"
new_value = row_list[13]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 殘疾人補(bǔ)貼
cell_reference = "D6"
new_value = row_list[8]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 轉(zhuǎn)移性收入
cell_reference = "D8"
new_value = row_list[22]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
# 非確權(quán)到戶的資產(chǎn)收益分紅
cell_reference = "D9"
new_value = row_list[15]
modify_cell_value(file_path, sheet_name, cell_reference, new_value)
lib\func.py
import shutil
import os
def copy_and_rename_file(source_file, destination_folder, new_file_name):
"""
復(fù)制文件到指定文件夾并以新名稱命名
參數(shù):
source_file (str):源文件的路徑
destination_folder (str):目標(biāo)文件夾的路徑
new_file_name (str):新的文件名
返回:
None
"""
# 檢查目標(biāo)文件夾是否存在酿联,如果不存在則創(chuàng)建
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
# 構(gòu)建目標(biāo)文件路徑
destination_file = os.path.join(destination_folder, new_file_name)
# 復(fù)制文件
shutil.copy(source_file, destination_file)
# source_file = "模板.xlsx"
# destination_folder = "test"
# new_file_name = "123.xlsx"
#
# copy_and_rename_file(source_file, destination_folder, new_file_name)
def modify_cell_value(file_path, sheet_name, cell_reference, new_value):
workbook = load_workbook(file_path)
sheet = workbook[sheet_name]
sheet[cell_reference] = new_value
workbook.save(file_path)
from openpyxl import load_workbook
from openpyxl.styles import Font
def underline_partial_text(file_path, sheet_name, cell_reference, start_index, end_index):
workbook = load_workbook(file_path)
sheet = workbook[sheet_name]
cell = sheet[cell_reference]
# 獲取原始文本
original_text = cell.value
# 拆分文本為三部分:前綴槐臀、要加下劃線的部分衩匣、后綴
prefix = original_text[:start_index]
underlined_part = original_text[start_index:end_index]
suffix = original_text[end_index:]
# 創(chuàng)建新的單元格內(nèi)容,將下劃線部分用特殊標(biāo)記包裹
new_text = prefix + f"__{underlined_part}__" + suffix
# 設(shè)置單元格的值為新的文本
cell.value = new_text
# 定義一個(gè)字體樣式逃延,用于識別特殊標(biāo)記并添加下劃線
underline_font = Font(underline='single')
def set_underline(cell, text):
if text.startswith("__") and text.endswith("__"):
cell.font = underline_font
return text[2:-2]
return text
# 遍歷單元格內(nèi)的每個(gè)字符
new_value = ""
for char in new_text:
new_value += set_underline(cell, char)
cell.value = new_value
workbook.save(file_path)
VBA(添加下劃線)套鹅,有意思的是給EXCEL同一個(gè)單元格中的部分文字添加下劃線python竟然無法實(shí)現(xiàn)瘾杭,這里智能使用VBA了
Sub UnderlinePartInAllWorkbooks()
Dim fso As Object
Dim folder As Object
Dim file As Object
Dim wb As Workbook
Dim ws As Worksheet
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("E:\WorkPro\扶貧填表\生成") '修改為您的文件夾路徑
For Each file In folder.Files
If Right(file.Name, 5) = ".xlsx" Or Right(file.Name, 4) = ".xls" Then
Set wb = Workbooks.Open(file.Path)
Set ws = wb.Sheets(1)
With ws.Range("A2").Characters(4, 12).Font '戶名
.Underline = True
End With
With ws.Range("A1").Characters(1, 3).Font '五指山
.Underline = True
End With
With ws.Range("A2").Characters(58, 3).Font '當(dāng)前家庭人口數(shù)
.Underline = True
End With
With ws.Range("A2").Characters(71, 3).Font '庭人口數(shù)
.Underline = True
End With
wb.Close SaveChanges:=True
End If
Next file
End Sub