Download utils/doc_utils.py from PerrinT/office: direct link, hf CLI and curl.
- Browser
- Download file 5.57 kB
-
https://huggingface.co/spaces/PerrinT/office/resolve/main/utils/doc_utils.py
- Command line
-
hf download hf://spaces/PerrinT/office/utils/doc_utils.py
-
curl -L -o doc_utils.py https://huggingface.co/spaces/PerrinT/office/resolve/main/utils/doc_utils.py
5.57 kB
| from docx import Document | |
| from docx.shared import Inches | |
| import io | |
| import qrcode | |
| from typing import Dict | |
| def generate_contract(template_type: str, data: Dict) -> bytes: | |
| """生成合同文档""" | |
| doc = Document() | |
| if template_type == "采购合同": | |
| doc.add_heading('采购合同', 0) | |
| doc.add_paragraph(f'合同编号:{data.get("contract_no", "")}') | |
| doc.add_paragraph(f'甲方(采购方):{data.get("party_a", "")}') | |
| doc.add_paragraph(f'乙方(供应方):{data.get("party_b", "")}') | |
| doc.add_paragraph(f'签订日期:{data.get("date", "")}') | |
| doc.add_paragraph('') | |
| doc.add_paragraph(f'一、采购物品:{data.get("item", "")}') | |
| doc.add_paragraph(f'二、数量:{data.get("quantity", "")}') | |
| doc.add_paragraph(f'三、单价:{data.get("unit_price", "")}') | |
| doc.add_paragraph(f'四、总金额:{data.get("total", "")}') | |
| doc.add_paragraph(f'五、交货日期:{data.get("delivery_date", "")}') | |
| doc.add_paragraph('') | |
| doc.add_paragraph('甲方签字:________________ 乙方签字:________________') | |
| elif template_type == "服务合同": | |
| doc.add_heading('服务合同', 0) | |
| doc.add_paragraph(f'合同编号:{data.get("contract_no", "")}') | |
| doc.add_paragraph(f'甲方(委托方):{data.get("party_a", "")}') | |
| doc.add_paragraph(f'乙方(服务方):{data.get("party_b", "")}') | |
| doc.add_paragraph(f'签订日期:{data.get("date", "")}') | |
| doc.add_paragraph('') | |
| doc.add_paragraph(f'一、服务内容:{data.get("service_content", "")}') | |
| doc.add_paragraph(f'二、服务期限:{data.get("service_period", "")}') | |
| doc.add_paragraph(f'三、服务费用:{data.get("fee", "")}') | |
| doc.add_paragraph(f'四、付款方式:{data.get("payment_method", "")}') | |
| doc.add_paragraph('') | |
| doc.add_paragraph('甲方签字:________________ 乙方签字:________________') | |
| elif template_type == "劳动合同": | |
| doc.add_heading('劳动合同', 0) | |
| doc.add_paragraph(f'甲方(用人单位):{data.get("company", "")}') | |
| doc.add_paragraph(f'乙方(劳动者):{data.get("employee", "")}') | |
| doc.add_paragraph(f'身份证号:{data.get("id_card", "")}') | |
| doc.add_paragraph(f'签订日期:{data.get("date", "")}') | |
| doc.add_paragraph('') | |
| doc.add_paragraph(f'一、工作岗位:{data.get("position", "")}') | |
| doc.add_paragraph(f'二、工作地点:{data.get("location", "")}') | |
| doc.add_paragraph(f'三、合同期限:{data.get("contract_period", "")}') | |
| doc.add_paragraph(f'四、薪资待遇:{data.get("salary", "")}') | |
| doc.add_paragraph('') | |
| doc.add_paragraph('甲方盖章:________________ 乙方签字:________________') | |
| buffer = io.BytesIO() | |
| doc.save(buffer) | |
| return buffer.getvalue() | |
| def mail_merge(template_text: str, data_list: list) -> list: | |
| """批量邮件合并""" | |
| results = [] | |
| for data in data_list: | |
| result = template_text | |
| for key, value in data.items(): | |
| result = result.replace(f'{{{{{key}}}}}', str(value)) | |
| results.append(result) | |
| return results | |
| def amount_to_chinese(amount: float) -> str: | |
| """金额转换为中文大写""" | |
| digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] | |
| units = ['', '拾', '佰', '仟'] | |
| big_units = ['', '万', '亿'] | |
| if amount == 0: | |
| return '零元整' | |
| # 分离整数和小数部分 | |
| amount = round(amount, 2) | |
| integer_part = int(amount) | |
| decimal_part = round((amount - integer_part) * 100) | |
| result = '' | |
| # 处理整数部分 | |
| if integer_part > 0: | |
| str_int = str(integer_part) | |
| length = len(str_int) | |
| for i, char in enumerate(str_int): | |
| digit = int(char) | |
| pos = length - i - 1 | |
| big_pos = pos // 4 | |
| unit_pos = pos % 4 | |
| if digit != 0: | |
| result += digits[digit] + units[unit_pos] | |
| else: | |
| if unit_pos == 0 and big_pos > 0: | |
| result += big_units[big_pos] | |
| elif result and not result.endswith('零'): | |
| result += '零' | |
| if unit_pos == 0 and big_pos > 0: | |
| result += big_units[big_pos] | |
| result = result.rstrip('零') + '元' | |
| # 处理小数部分 | |
| if decimal_part > 0: | |
| jiao = decimal_part // 10 | |
| fen = decimal_part % 10 | |
| if jiao > 0: | |
| result += digits[jiao] + '角' | |
| if fen > 0: | |
| result += digits[fen] + '分' | |
| else: | |
| result += '整' | |
| return result | |
| def generate_qrcode(data: str, size: int = 10) -> bytes: | |
| """生成二维码""" | |
| qr = qrcode.QRCode(version=1, box_size=size, border=2) | |
| qr.add_data(data) | |
| qr.make(fit=True) | |
| img = qr.make_image(fill_color="black", back_color="white") | |
| buffer = io.BytesIO() | |
| img.save(buffer, format='PNG') | |
| return buffer.getvalue() | |
| def generate_barcode(data: str, barcode_type: str = "code128") -> bytes: | |
| """生成条形码""" | |
| try: | |
| import barcode | |
| from barcode.writer import ImageWriter | |
| code_class = barcode.get_barcode_class(barcode_type) | |
| code = code_class(data, writer=ImageWriter()) | |
| buffer = io.BytesIO() | |
| code.write(buffer) | |
| return buffer.getvalue() | |
| except ImportError: | |
| # 如果没有python-barcode库,返回None | |
| return None | |