----------->>>>>>>>>>>>>>>>>>>>>>>>>>>import requests
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>#import xlwt
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>import pdfkit
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>import openpyxl
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>workbook = openpyxl.load_workbook('2020经销商目标.xlsx')
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>sheet1 = workbook['Sheet1']
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>print(sheet1)
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>html = """
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>><!doctype html>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>><html lang="en">
----------->>>>>>>>>>>>>>>>>>>>>>>>>>><head>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>><title>Document</title>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>><style>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> table {
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> font-size : 22px;
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> width: 850px;
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> }
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> .blod {
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> font_weight: bolder;
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> }
----------->>>>>>>>>>>>>>>>>>>>>>>>>>></style>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>></head>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>><body>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>><table border = "1" align = "center">
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <tr>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td colspan ="6" align = "center" >2020年广东经销商预算目标</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> </tr>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <tr>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td>经销商代码</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td>经销商名称</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td>成交数量</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td>成交金额</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td>商品金额</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td>客户签字</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> </tr>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <tr class = "blod">
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td>code</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td>name</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td>number</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td>money</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td>total</td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> <td></td>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> </tr>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>></table>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>></body>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>></html>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>"""
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>for row in list(sheet1.rows)[3:]:
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> for col in row:
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> print(col.value,end='\t')
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> print()
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> code = row[0].value
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> name = row[1].value
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> number = row[2].value
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> money = row[3].value
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> total = row[4].value
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> print(code,name)
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> html = html.replace('code',f'{code}')
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> html = html.replace('name',f'{name}')
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> html = html.replace('number',f'{number}')
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> html = html.replace('money',f'{money}')
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> html = html.replace('total',f'{total}')
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> #print(html)
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> with open('example.html',encoding='utf-8',mode = 'w') as f:
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> f.write(html)
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> #with open('example.html',encoding='utf-8',mode = 'r') as f:
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> # html = f.read()
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> ##--#配置pdf的转化
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> config = pdfkit.configuration(wkhtmltopdf=r'E:\青青教育\前程无忧\wkhtmltopdf\wkhtmltopdf\bin\wkhtmltopdf.exe')
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> #from_file
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> pdfkit.from_file('example.html',f'{name}.pdf',configuration=config)
----------->>>>>>>>>>>>>>>>>>>>>>>>>>>
----------->>>>>>>>>>>>>>>>>>>>>>>>>>> break
-----<Worksheet "Sheet1">
-----2020年广东经销商预算目标 None None None None None
-----None None None None None None
-----经销商代码 经销商名称 成交数量 成交金额 商品金额 客户签字
-----AA111 一一一 156 123123 123123 None
-----<Worksheet "Sheet1">
-----None None None None None
-----经销商名称 成交数量 成交金额 商品金额 客户签字
-----一一一 156 123123 123123 None
def func(money):
#print(money)
str_number = f'{money}'
str_list = list(str_number[::-1]) ##### 顺序反转
#print(str_list)
str_ = ''
i = 1
for no , char in enumerate(str_list):
str_ += char
#print(no , char)
if i % 3 == 0 and len(str_list)!=i:
str_ += ','
i+=1
#print(str_[::-1])
return str_[::-1]
if name == 'main':
#func('24578.13') 24,578,.13
#func('224578') #24,578 224,578
#func('224578') ,224,578
https://pdf2000.com/converter/exceltopdf/?utm_source=baidunbz02&utm_medium=gj-PDFzh-Excel&utm_campaign=PDFzh-Excel-zh&utm_term=pythonexcelzpdf&e_keywordid=416335210836&e_keywordid2=416335210836&bd_vid=7870719357992339623
https://pdf2000.com/converter/exceltopdf/
https://www.bilibili.com/video/BV1SL411c7oX/?spm_id_from=pageDriver
https://www.bilibili.com/video/BV1SL411c7oX/?spm_id_from=pageDriver
https://www.bilibili.com/video/BV1SL411c7oX/?spm_id_from=pageDriver
https://www.bilibili.com/video/BV1SL411c7oX/?spm_id_from=pageDriver
求助使用python将excel转换成PDF的方法
千次阅读
2020-12-03 11:11:23
我用的是win32com的三方库,转换没有问题,只是excel的内容比较多,
最终转换完成后的样式不是自己需要的,请问ExportAsFixedFormat()方法中有没有
什么属性是可以将整个sheet页的内容转换成一张PDF,求大佬救命。
Import Module
from win32com import client
Open Microsoft Excel
excel = client.Dispatch("Excel.Application")
Read Excel File
sheets = excel.Workbooks.Open('2020经销商目标.xlsx')
work_sheets = sheets.Worksheets[0]
Convert into PDF File
work_sheets.ExportAsFixedFormat(0, '2020经销商目标.pdf')