代码拉取完成,页面将自动刷新
from docx import Document
from docx.shared import Pt
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
from docx.enum.text import WD_ALIGN_PARAGRAPH # 设置对象居中、对齐等。
from docx.enum.table import WD_ROW_HEIGHT_RULE
from docx.enum.table import WD_ALIGN_VERTICAL
import sys,os
import datetime
def Num2Big(money):
cnNums = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"] #汉字的数字
cnIntRadice = ["", "拾", "佰", "仟"] #基本单位
cnIntUnits = ["", "万", "亿", "兆"] #对应整数部分扩展单位
cnDecUnits = ["角", "分", "毫", "厘"] #对应小数部分单位
cnInteger = "整" #整数金额时后面跟的字符
cnIntLast = "元" #整型完以后的单位
maxNum = 999999999999999.9999 #最大处理的数字
# IntegerNum 金额整数部分
# DecimalNum 金额小数部分
ChineseStr = "" #输出的中文金额字符串
parts =[] #分离金额后用的数组,预定义
Symbol = "" #正负值标记
if money == "":
return ""
money = float(money)
if money >= maxNum:
return ""
if money == 0:
ChineseStr = cnNums[0] + cnIntLast + cnInteger
return ChineseStr
if money < 0:
money = -money
Symbol = "负 "
money = str(money) #转换为字符串
if money.find(".") == -1:
IntegerNum = money
DecimalNum = ""
else:
parts = money.split(".")
IntegerNum = parts[0]
DecimalNum = parts[1][0:4]
if int(IntegerNum) > 0:#获取整型部分转换
zeroCount = 0
IntLen = len(IntegerNum)
for i in range(0,IntLen):
n = IntegerNum[i:i+1:1]
p = IntLen - i - 1
q = p // 4
m = p % 4
if n == "0":
zeroCount +=1
else:
if zeroCount > 0:
ChineseStr += cnNums[0]
zeroCount = 0 #归零
ChineseStr += cnNums[int(n)] + cnIntRadice[m]
if m == 0 and zeroCount < 4:
ChineseStr += cnIntUnits[q]
ChineseStr += cnIntLast#整型部分处理完毕
if DecimalNum != "":#小数部分
decLen = len(DecimalNum)
for i in range(0,decLen):
n = DecimalNum[i:i+1:1]
if n != "0":
ChineseStr += cnNums[int(n)] + cnDecUnits[i]
if ChineseStr == "":
ChineseStr += cnNums[0] + cnIntLast + cnInteger
elif DecimalNum == "0":
ChineseStr += cnInteger
ChineseStr = Symbol + ChineseStr
return ChineseStr
def set_cell_border(cell, **kwargs):
"""
Set cell`s border
Usage:
set_cell_border(
cell,
top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},
bottom={"sz": 12, "color": "#00FF00", "val": "single"},
left={"sz": 24, "val": "dashed", "shadow": "true"},
right={"sz": 12, "val": "dashed"},
)
"""
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
# check for tag existnace, if none found, then create one
tcBorders = tcPr.first_child_found_in("w:tcBorders")
if tcBorders is None:
tcBorders = OxmlElement('w:tcBorders')
tcPr.append(tcBorders)
# list over all available tags
for edge in ('left', 'top', 'right', 'bottom', 'insideH', 'insideV'):
edge_data = kwargs.get(edge)
if edge_data:
tag = 'w:{}'.format(edge)
# check for tag existnace, if none found, then create one
element = tcBorders.find(qn(tag))
if element is None:
element = OxmlElement(tag)
tcBorders.append(element)
# looks like order of attributes is important
for key in ["sz", "val", "color", "space", "shadow"]:
if key in edge_data:
element.set(qn('w:{}'.format(key)), str(edge_data[key]))
def addrow(table, times):
"""
添加表格行
:param table: 表格名
:param times: 添加几行
"""
for i in range(times):
table.add_row()
insertion_row = table.rows[5]._tr
insertion_row.addnext(table.rows[-1]._tr)
# 合并不规范的单元格
table.cell(6, 1).merge(table.cell(6, 3))
# 设置单元格边框
for j in range(9):
set_cell_border(table.cell(6, j),
top={"sz": 4, "val": "single", "color": "#000000", "space": "0"},
bottom={"sz": 4, "val": "single", "color": "#000000", "space": "0"},
left={"sz": 4, "val": "single", "color": "#000000", "space": "0"},
right={"sz": 4, "val": "single", "color": "#000000", "space": "0"}, )
def SetDetail(table,lists):
addrow(table, len(lists)-1)
# 明细复制赋值
for row in range(len(lists)):
# 需要指定单元格的大小
table.rows[row + 5].height_rule = WD_ROW_HEIGHT_RULE.EXACTLY
table.rows[row + 5].height = Pt(24)
for col in range(9):
table.cell(row + 5, col).paragraphs[0].clear()
table.cell(row + 5, col).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
cell_font = table.cell(row + 5, col).paragraphs[0].add_run(lists[row][col])
# 设置单元格居中
table.cell(row + 5, col).vertical_alignment = WD_ALIGN_VERTICAL.CENTER
# 设置英文字体为Times New Roman
cell_font.font.name = 'Times New Roman'
# 设置字体大小
cell_font.font.size = Pt(12)
# 设置中文字体为宋体
cell_font._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
def SetTime(table):
#清空表格里的时间#
table.cell(1,3).paragraphs[0].clear() #序号
#读取时间
time=datetime.datetime.now().strftime('%Y年%m月%d日')
#修改表格的时间
run_table_time = table.cell(1,3).paragraphs[0].add_run(str(time)) #序号
#设置英文字体为Times New Roman
run_table_time.font.name = 'Times New Roman'
#设置字体大小
run_table_time.font.size = Pt(12)
#设置中文字体为宋体
run_table_time._element.rPr.rFonts.set(qn('w:eastAsia'),'宋体')
def SetCompanyName(table,company_name):
#清空#
table.cell(3,1).paragraphs[0].clear() #序号
#修改
run_table_time = table.cell(3,1).paragraphs[0].add_run(company_name) #序号
#设置英文字体为Times New Roman
run_table_time.font.name = 'Times New Roman'
#设置字体大小
run_table_time.font.size = Pt(12)
#设置中文字体为宋体
run_table_time._element.rPr.rFonts.set(qn('w:eastAsia'),'宋体')
def SetNum(table,lists):
# 计算所有的总数
allnum = 0.0
for index in lists:
allnum += float(index[-1])
# 保留2位小数点
allnum = str(format(allnum, ".2f"))
#清空总价大写数字
table.cell(-5,8).paragraphs[0].clear()
#修改表格的总价大写数字
run_table_allnum = table.cell(-5,8).paragraphs[0].add_run(allnum) #序号
#设置英文字体为Times New Roman
run_table_allnum.font.name = 'Times New Roman'
#设置字体大小
run_table_allnum.font.size = Pt(12)
#清空总价大写数字
table.cell(-5,4).paragraphs[0].clear()
#读取发票的总价
num=float(allnum)
#修改表格的总价大写数字
run_table_bignum = table.cell(-5,4).paragraphs[0].add_run(str(Num2Big(num))) #序号
#设置英文字体为Times New Roman
run_table_bignum.font.name = 'Times New Roman'
#设置字体大小
run_table_bignum.font.size = Pt(12)
#设置中文字体为宋体
run_table_bignum._element.rPr.rFonts.set(qn('w:eastAsia'),'宋体')
#测试数据
# lists=[['1', ' ', ' ', '123', '赛 ', '个', '1', '123', '123.7'],
# ['2', ' ', ' ', '34', '赛发动发', '个', '213', '3', '639.6'],
# ['3', ' ', ' ', '东方', '送的', '个', '2', '4', '8'],
# ['4', ' ', ' ', '懂发', '赛', '个', '1', '4', '4'],
# ['5', ' ', ' ', '懂发', '赛', '个', '1', '4', '4'],
# ['6', ' ', ' ', '懂发', '赛', '个', '1', '4', '4'],
# ['7', ' ', ' ', '懂发', '赛', '个', '1', '4', '4'],
# ['8', ' ', ' ', '懂发', '赛', '个', '1', '4', '4']]
# company_name="fsd公司"
# name="螺1丝"
def CreatForms(name,company_name,lists):
pwd_path=os.getcwd()
in_forms_file_path = os.path.join(pwd_path,"template/1.docx")
out_forms_file_path = os.path.join(pwd_path,"template/2.docx")
document_in_forms = Document(in_forms_file_path)
document_out_forms = Document(out_forms_file_path)
##入库单
# 读取第一个表格
table = document_in_forms.tables[0]
SetDetail(table,lists)
SetTime(table)
SetCompanyName(table,company_name)
SetNum(table,lists)
##出库单
table = document_out_forms.tables[0]
SetDetail(table,lists)
SetTime(table)
SetNum(table,lists)
# 保存文档
in_pre="入库单-"
document_in_forms.save(in_pre+name+".docx")
out_pre="出库单-"
document_out_forms.save(out_pre+name+".docx")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。