本文整理汇总了Python中docxtpl.DocxTemplate.get_docx方法的典型用法代码示例。如果您正苦于以下问题:Python DocxTemplate.get_docx方法的具体用法?Python DocxTemplate.get_docx怎么用?Python DocxTemplate.get_docx使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docxtpl.DocxTemplate
的用法示例。
在下文中一共展示了DocxTemplate.get_docx方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from docxtpl import DocxTemplate [as 别名]
# 或者: from docxtpl.DocxTemplate import get_docx [as 别名]
def main():
config = configparser.ConfigParser()
config.read('autotemplation.ini')
template_folder_ids = config['DEFAULT']['TemplateFolderID'].split(',')
destination_folder_name = config['DEFAULT']['DestinationFolderName']
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
drive_service = discovery.build('drive', 'v3', http=http)
destination_folder_id = get_or_create_destination_folder_id(
drive_service, destination_folder_name)
template_file = get_template(drive_service, template_folder_ids)
mime_type, is_sheet = get_mime_type(template_file['mimeType'])
request = drive_service.files().export_media(
fileId=template_file['id'],
mimeType=mime_type)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
if is_sheet:
print("Spreadsheet selected, converting to Doc. (Slow)")
table_data = get_sheet_data(fh)
row_count = len(table_data)
col_count = len(table_data[0])
document = Document()
doc_table = document.add_table(rows=row_count,
cols=col_count)
for r, row in enumerate(table_data):
row_cells = doc_table.rows[r].cells
print("Converting row {}/{}...".format(r+1, row_count), end="\r")
for i, cell in enumerate(row):
if cell:
row_cells[i].text = cell
print("Conversion complete. "
"Warning: Processing large sheets will take some time.")
temp_doc_file = io.BytesIO()
document.save(temp_doc_file)
doc = DocxTemplate(temp_doc_file)
else:
doc = DocxTemplate(fh)
full_doc = doc.get_docx()
template_vars = get_template_variables(full_doc, template_file['name'])
if any('__' in x for x in template_vars):
worksheet = get_worksheet(credentials)
worksheet_headers = get_worksheet_headers(worksheet)
context = dict()
get_date_and_set_context(context)
for var in template_vars:
if var not in context:
if '__' in var:
context[var] = worksheet_lookup(
worksheet, worksheet_headers, var)
else:
context[var] = input("Enter a value for {}: ".format(var))
new_file_name = get_target_name(template_file['name'], context)
doc.render(context)
temp_file = tempfile.NamedTemporaryFile()
doc.save(temp_file)
if is_sheet:
csv_name = '{}.csv'.format(new_file_name)
doc_csv = DocxTemplate(temp_file)
csv_data = get_table_data_for_csv(doc_csv)
if csv_data:
with open(csv_name, 'w') as output:
writer = csv.writer(output, lineterminator='\n')
writer.writerows(csv_data)
print('{} created in local folder'.format(csv_name))
else:
print('Unable to create CSV. '
'Less than or more than 1 table found.')
workbook = Workbook()
sheet = workbook.get_active_sheet()
for row in csv_data:
sheet.append(row)
workbook.save(temp_file)
upload_mimetype = 'application/vnd.google-apps.spreadsheet'
else:
upload_mimetype = 'application/vnd.google-apps.document'
file_metadata = {
'name': new_file_name,
'parents': [destination_folder_id],
'mimeType': upload_mimetype
}
media = MediaFileUpload(temp_file.name,
mimetype=mime_type,
resumable=True)
drive_service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print('{} placed in folder {}.'.format(new_file_name,
destination_folder_name))
temp_file.close()