def reportAddendum(reportFile, listofOutputsTobeAdded, listOfComments=None): """ Enables to add figures to a report in an automated way. The changes intervine after the last chapter of the report. """ if ".docx" in reportFile: document = Document(reportFile) else: raise Warning(" problem with the format of the file") paragraph_styles = [s for s in document.styles if s.type == WD_STYLE_TYPE.PARAGRAPH] postProcessingHeader = document.add_paragraph("PostProcessing", style=paragraph_styles[1]) postProcessingText = document.add_paragraph(style=paragraph_styles[0]) postProcessingText.add_run("\rWord format postprocessing generated through the ") postProcessingText.add_run("etumos ").bold = True postProcessingText.add_run("coupling tool\r\r") # document.add_heading('Outputs', level=1) ind = 0 for picture in listofOutputsTobeAdded: document.add_picture(picture, width=Cm(7.00)) document.paragraphs[-1].alignment = WD_ALIGN_PARAGRAPH.CENTER newParagraph = document.add_paragraph("\r") newParagraph.add_run("Figure n. " + str(ind) + ": ").bold = True if listOfComments != None: newParagraph.add_run(str(listOfComments[ind])) ind += 1 newParagraph.add_run(" \r \r") newParagraph.alignment = WD_ALIGN_PARAGRAPH.CENTER reportFile = reportFile.replace(".docx", "") filenameToBeSaved = reportFile + "_pp.docx" document.save(filenameToBeSaved) return None
def insetImgToDocx(image): document = Document(docx_file) p = document.add_paragraph() r = p.add_run() r.add_picture(image, width=Inches(5.5)) #inces sesuai yang ada di garis r.add_text('Ini picture: {}'.format(image)) document.save(docx_file)
def exportPayInfo(self): """ 导出出付费信息 """ title = '%s[%s]' % (self.username, datetime.strftime(datetime.now(), "%Y-%m-%d")) reserveInfo = pitcher.getReserveInfo() c1 = reserveInfo[u'状态'] == u'未确认' c2 = reserveInfo[u'最后确认时间'].apply(parser.parse) > datetime.now() reserveInfo = reserveInfo[c1 & c2] document = Document() document.add_heading(title) for i, row in reserveInfo.iterrows(): document.add_heading(u'票项%d' % (i + 1), level=1) document.add_paragraph(text=u'航线: ' + row[u'航线']) document.add_paragraph(text=u'航班时间: ' + row[u'航班时间']) document.add_paragraph(text=u'人数: ' + row[u'人数']) document.add_paragraph(text=u'金额: ' + row[u'金额']) document.add_paragraph(text=u'最后确认时间: ' + row[u'最后确认时间']) filename = tempfile.mktemp(suffix='.jpg',prefix='tmp_') with open(filename, 'wb') as f: orderNumber = pitcher.getOrderNumber(row[u'预订ID']) qrcode = pitcher.getWeixinPayQrcode(orderNumber) f.write(qrcode) document.add_picture(filename, width=Inches(1)) time.sleep(self.normalWaitingSecond) filename = tempfile.mktemp(suffix='.docx',prefix=title + '_') document.save(filename) # 发送邮件 send_mail(settings.MAIL_LIST, title, u'见附件', [filename])
def certificate(request, training_id, ppant_id): t = Training.objects.get(pk=training_id) p = Participant.objects.get(pk=ppant_id) document = Document('media/template.docx') for paragraph in document.paragraphs: for run in paragraph.runs: if '<<name>>' in run.text: if p.mi: run.text = p.sname + ', ' + p.fname + ' ' + p.mi else: run.text = p.sname + ', ' + p.fname if '<<module>>' in run.text: run.text = '"' + t.module.fullname + '"' if '<<date>>' in run.text: startdate = datetime.datetime.strptime(str(t.date), '%Y%m%d') enddate = startdate + datetime.timedelta(days=4) run.text = 'held from ' + startdate.strftime('%d %B') + ' to ' + enddate.strftime('%d %B %Y') + ' at the ' if '<<location>>' in run.text: l = Institution.objects.get(abbrev=t.location) run.text = l.fullname + ', ' + l.city + ', ' + l.province if '<<honors>>' in run.text: run.text = '' if '<<serial>>' in run.text: code = 'participant'+ppant_id+'training'+training_id serial = encrypt(code) run.text = 'Serial: ' + serial if '<<verification>>' in run.text: run.text = 'The authenticity of this certificate can be verified at\nhttp://db.portal.piic.org.ph/verif/' + serial + '/' document.save('media/'+serial+'.docx') os.system('libreoffice --headless --convert-to pdf media/'+serial+'.docx') os.system('mv '+serial+'.pdf static/cert/'+serial+'.pdf') os.system('rm media/'+serial+'.docx') Message.objects.create(participant=p,category=serial,medium='cert') url = static('cert/'+serial+'.pdf') return HttpResponseRedirect(url)
def create_completed_review_form(submission,review_id): document = Document() document.add_heading(submission.title, 0) review_assignment = get_object_or_404(core_models.ReviewAssignment, pk=review_id) if review_assignment.review_form: relations = models.FormElementsRelationship.objects.filter(form=review_assignment.review_form).order_by('order') else: review_assignment.review_form = submission.review_form review_assignment.save() relations = models.FormElementsRelationship.objects.filter(form=submission.review_form).order_by('order') if review_assignment.results: p = document.add_paragraph('%s completed this review assignment form.'% review_assignment.user.profile.full_name()) data = json.loads(review_assignment.results.data) for relation in relations: v = data[relation.element.name] document.add_heading(relation.element.name, level=1) text = BeautifulSoup(str(v[0]),"html.parser").get_text() document.add_paragraph(text).bold = True recommendations = {'accept': 'Accept','reject': 'Reject', 'revisions':'Revisions Required'} document.add_heading("Recommendation", level=1) document.add_paragraph(recommendations[review_assignment.recommendation]).italic = True document.add_heading("Competing Interests", level=1) document.add_paragraph(review_assignment.competing_interests).italic = True else: p = document.add_paragraph('You should complete this form and then use the review assignment page to upload it.') for relation in relations: if relation.element.field_type in ['text', 'textarea', 'date', 'email']: document.add_heading(relation.element.name+": _______________________________", level=1) document.add_paragraph(relation.help_text).italic = True if relation.element.field_type in ['select', 'check']: document.add_heading(relation.element.name, level=1) if relation.element.field_type == 'select': choices = render_choices(relation.element.choices) else: choices = ['Y', 'N'] p = document.add_paragraph(relation.help_text) p.add_run(' Mark your choice however you like, as long as it is clear.').italic = True table = document.add_table(rows=2, cols=len(choices)) hdr_cells = table.rows[0].cells for i, choice in enumerate(choices): hdr_cells[i].text = choice[0] table.style = 'TableGrid' document.add_page_break() if not os.path.exists(os.path.join(settings.BASE_DIR, 'files', 'forms')): os.makedirs(os.path.join(settings.BASE_DIR, 'files', 'forms')) path = os.path.join(settings.BASE_DIR, 'files', 'forms', '%s.docx' % str(uuid4())) document.save(path) return path
def create_review_form(submission, review_form): document = Document() document.add_heading(submission.title, 0) p = document.add_paragraph('You should complete this form and then use the review page to upload it.') relations = models.FormElementsRelationship.objects.filter(form=review_form).order_by('order') for relation in relations: if relation.element.field_type in ['text', 'textarea', 'date', 'email']: document.add_heading(relation.element.name+": _______________________________", level=1) document.add_paragraph(relation.help_text).italic = True if relation.element.field_type in ['select', 'check']: document.add_heading(relation.element.name, level=1) if relation.element.field_type == 'select': choices = render_choices(relation.element.choices) else: choices = ['Y', 'N'] p = document.add_paragraph(relation.help_text) p.add_run(' Mark your choice however you like, as long as it is clear.').italic = True table = document.add_table(rows=2, cols=len(choices)) hdr_cells = table.rows[0].cells for i, choice in enumerate(choices): hdr_cells[i].text = choice[0] table.style = 'TableGrid' document.add_page_break() if not os.path.exists(os.path.join(settings.BASE_DIR, 'files', 'forms')): os.makedirs(os.path.join(settings.BASE_DIR, 'files', 'forms')) path = os.path.join(settings.BASE_DIR, 'files', 'forms', '%s.docx' % str(uuid4())) document.save(path) return path
def modifyDocx(filePath, values): doc = Document(filePath) a,p = findField(doc, "total_tf", range(30, 40), range(5, 10)) fillVal(doc, a, p, values.totalWVAT) a,p = findField(doc, "total_vat", range(30, 40), range(5, 20)) fillVal(doc, a, p, values.totalVAT) a,p = findField(doc, "total", range(30, 40), range(10, 20)) fillVal(doc, a, p, values.total) a,p = findField(doc, "travel", range(30, 40), range(10, 20)) fillVal(doc, a, p, values.travel) a,p = findField(doc, "car", range(30, 40), range(10, 20)) fillVal(doc, a, p, values.travelByCar) a,p = findField(doc, "accom", range(30, 40), range(10, 20)) fillVal(doc, a, p, values.accomodation) a,p = findField(doc, "accom_vat", range(30, 40), range(20, 30)) fillVal(doc, a, p, values.accomodationVAT) a,p = findField(doc, "food", range(30, 40), range(20, 30)) fillVal(doc, a, p, values.subsistence) a,p = findField(doc, "food_vat", range(30, 40), range(20, 30)) fillVal(doc, a, p, values.subsistenceVAT) a,p = findField(doc, "taxi", range(30, 40), range(20, 30)) fillVal(doc, a, p, values.taxi) a,p = findField(doc, "taxi_vat", range(30, 40), range(20, 30)) fillVal(doc, a, p, values.taxiVAT) a,p = findField(doc, "fuel", range(30, 40), range(20, 30)) fillVal(doc, a, p, values.other) a,p = findField(doc, "fuel_vat", range(30, 40), range(20, 30)) fillVal(doc, a, p, values.otherVAT) a,p = findField(doc, "total_b", range(30, 40), range(30, 40)) fillVal(doc, a, p, values.total) doc.save("test2.docx")
def stickers_transform(): """Transforms the CSV data into a DOCX file""" document = Document('landscape.docx') with open('master9.7.csv', 'rU') as infile: reader = csv.reader(infile) table = document.add_table(2,2) i = 0 for row in reader: index1, index2 = i / 2, i % 2 cell = table.cell(index1, index2) prod = row[3] write_line_in_cell(cell, '\n', 20) write_line_in_cell(cell, row[1]) write_line_in_cell(cell, row[2]) write_line_in_cell(cell, row[0]) write_line_in_cell(cell, row[3]) write_line_in_cell(cell, '\n', 20) i += 1 if i % 4 == 0: table = document.add_table(2,2) i = 0 document.save('labels.docx')
def generate_documents(recipes): create_output_dir() for recipe in recipes: doc = Document() heading_r = doc.add_paragraph().add_run(recipe.title.string) heading_r.font.size = Pt(14) heading_r.font.bold = True heading_r.font.underline = True ingredients_p = doc.add_paragraph() ingredients_r = ingredients_p.add_run("Zutaten") ingredients_r.font.size = Pt(13) ingredients_r.font.bold = True for ingredient in recipe.ingredients: ingredient_li_p = doc.add_paragraph(style = "ListBullet") ingredient_li_p.add_run(ingredient).font.size = Pt(13) instructions_p = doc.add_paragraph() instructions_r = instructions_p.add_run("Zubereitung") instructions_r.font.size = Pt(13) instructions_r.font.bold = True for text in recipe.texts: text_r = doc.add_paragraph().add_run(text) text_r.font.size = Pt(13) doc.save(os.path.join(output_dir, recipe.title.string + ".docx"))
def scan_file(path, folder, c): print(path+"\n") document = Document(path) for table in document.tables: if folder == "generale": for row in table.rows: # codice|question|a|b|c|d|answer print("add " + row.cells[0].text) insert = (row.cells[0].text, row.cells[1].text, row.cells[2].text, row.cells[3].text, row.cells[4].text, row.cells[5].text, row.cells[6].text, folder) c.execute("INSERT INTO domande VALUES (?,?,?,?,?,?,?,?)", insert) elif folder == "sunto": for row in table.rows: # codice|question|a|b|c|d|answer print("add " + row.cells[0].text) insert = (row.cells[0].text, row.cells[1].text, row.cells[2].text, row.cells[3].text, row.cells[4].text, row.cells[5].text, row.cells[6].text, folder) c.execute("INSERT INTO domande VALUES (?,?,?,?,?,?,?,?)", insert) elif folder == "comprensione": for row in table.rows: # codice|testo(span 5)|empty # codice|question|a|b|c|d|answer if re.search('[A-Z]{2}[0-9]{3}0{2}', row.cells[0].text): # is the text print("add " + row.cells[0].text) insert = (row.cells[0].text, row.cells[1].text, None, None, None, None, None, "comprensione_a") c.execute("INSERT INTO domande VALUES (?,?,?,?,?,?,?,?)", insert) else: #question over the text print("add " + row.cells[0].text) insert = (row.cells[0].text, row.cells[1].text, row.cells[2].text, row.cells[3].text, row.cells[4].text, row.cells[5].text, row.cells[6].text, "comprensione_b") c.execute("INSERT INTO domande VALUES (?,?,?,?,?,?,?,?)", insert) document.save(path)
def WriteDoc(self, song_title_list, song_lyric_dict): document = Document("template.docx") for index, title in enumerate(song_title_list): #p = document.add_paragraph(TITLE_PREFIX + title).bold = True p = "" if index == 0: p = document.paragraphs[0] else: p = document.add_paragraph() run = p.add_run(TITLE_PREFIX + title) font = run.font font.bold = True #font.name = 'SimSun' font.name = 'Microsoft YaHei' font.size = doc_Pt(12) #p = document.add_paragraph() song_lyric_paragraph_list = song_lyric_dict.get(title) if song_lyric_paragraph_list == None: continue for song_lyric_paragraph in song_lyric_paragraph_list: for song_lyric_ in song_lyric_paragraph: p = document.add_paragraph() run = p.add_run(song_lyric_) font = run.font font.size = doc_Pt(12) p = document.add_paragraph() document.save(today + '.docx') return
def deal_doc_task(): file_db_base = 'd:/xq/xqfav.db' base_db_instance = sqlite3.connect(file_db_base) base_db_cursor = base_db_instance.cursor() document = Document() # base_db_cursor.execute(' select pid, title, created_at, userid, username, content from xq_fav order by username, pid') lists = base_db_cursor.fetchall() for vals in lists: # title document.add_heading( '%s - %s @%s' % (vals[0], vals[1], vals[4]), 0 ) # content #try: generate_xq_content( document, vals[5] ) #except: # print '!--generate failed! %s' % vals[0] # next page document.add_page_break() # # doc_file = 'D:/xq/HiRoad_XQFav_%s.docx' % datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") document.save(doc_file)
class XQHTMLParser(HTMLParser): def __init__(self, docfile): HTMLParser.__init__(self) self.docfile = docfile self.doc = Document(docfile) self.myclient = HTMLClient() self.text = '' self.title = False self.isdescription = False self.picList=[] def handle_starttag(self, tag, attrs): #print "Encountered the beginning of a %s tag" % tag self.title = False self.isdescription = False if re.match(r'h(\d)', tag): self.title = True if tag == "img": if len(attrs) == 0: pass else: for (variable, value) in attrs: if variable == "src": picdata = self.myclient.GetPic(value.split('!')[0]) if picdata == None: pass else: pictmp = value.split('/')[-1].split('!')[0] picfix = value.split('/')[-1].split('!')[-1] with open(pictmp, 'wb') as pic: pic.write(bytes(picdata)) pic.close() #if os.path.getsize(pictmp) < 90000: try: if picfix[0:1] == 'c': self.doc.add_picture(pictmp, width=Inches(4.5)) else: self.doc.add_picture(pictmp)#, width=Inches(2.25)) except docx.image.exceptions.UnexpectedEndOfFileError as e: print(e) self.picList.append(pictmp) if tag == 'script': self.isdescription = True def handle_data(self, data): if self.title == True: if self.text != '': self.doc.add_paragraph(self.text) self.text = '' self.doc.add_heading(data, level=2) if self.isdescription == False: self.text += data def handle_endtag(self, tag): #if tag == 'br' or tag == 'p' or tag == 'div': if self.text != '': self.doc.add_paragraph(self.text) self.text = '' def complete(self, html): self.feed(html) self.doc.save(self.docfile) for item in self.picList: if os.path.exists(item): os.remove(item)
def get_brief_name(expression, con): # Fetch all kinds of object_name from all_objects find_object_info = '''select OBJECT_TYPE, OBJECT_NAME, OWNER from ALL_OBJECTS where regexp_like(OBJECT_NAME ,:expression)''' cur = con.cursor() cur.execute(find_object_info, {'expression': str(expression)}) object_info = cur.fetchall() view_name = Document() index_name = Document() package_name = Document() trigger_name = Document() sequence_name = Document() for res in object_info: # print res if str(res[0]) == 'VIEW': view_name.add_paragraph(str(res[1])) if str(res[0]) == 'INDEX': index_name.add_paragraph(str(res[1])) if str(res[0]) == 'PACKAGE': package_name.add_paragraph(str(res[1])) if str(res[0]) == 'TRIGGER': trigger_name.add_paragraph(str(res[1])) if str(res[0]) == 'SEQUENCE': sequence_name.add_paragraph(str(res[1])) view_name.save('ViewName.docx') index_name.save('IndexName.docx') package_name.save('PackageName.docx') trigger_name.save('TriggerName.docx') sequence_name.save('SequenceName.docx')
def download(rfq_id): # send_file? document = Document() session = Session() rfq = session.query(RFQ).filter_by(id=rfq_id).first() print rfq title = "RFQ for " + rfq.agency document.add_heading("RFQ", 0) p = document.add_paragraph("A plain paragraph having some ") p.add_run("bold").bold = True p.add_run(" and some ") p.add_run("italic.").italic = True document.add_heading("Heading, level 1", level=1) document.add_paragraph("Intense quote", style="IntenseQuote") document.add_paragraph("first item in unordered list", style="ListBullet") document.add_paragraph("first item in ordered list", style="ListNumber") doc_name = "RFQ_" + str(rfq_id) + ".docx" file_path = os.path.join(doc_name, "downloads") document.save(file_path) # download_folder = os.path.join(current_app.root_folder, "downloads") return send_from_directory(directory="downloads", filename=doc_name)
def mdoc(): document = Document() document.add_heading("Document Title", 0) p = document.add_paragraph("A plain paragraph having some ") p.add_run("bold").bold = True p.add_run(" and some ") p.add_run("italic.").italic = True document.add_heading("Heading, level 1", level=1) document.add_paragraph("Intense quote", style="IntenseQuote") document.add_paragraph("first item in unordered list", style="ListBullet") document.add_paragraph("first item in ordered list", style="ListNumber") document.add_picture("monty-truth.png", width=Inches(1.25)) table = document.add_table(rows=1, cols=3) hdr_cells = table.rows[0].cells hdr_cells[0].text = "Qty" hdr_cells[1].text = "Id" hdr_cells[2].text = "Desc" for item in recordset: row_cells = table.add_row().cells row_cells[0].text = str(item.qty) row_cells[1].text = str(item.id) row_cells[2].text = item.desc document.add_page_break() document.save("demo.docx")
def WriteTiezi(self): if len(self.pages)==0: print 'Error!' document = Document() # style = document.StyleSheet # style.Fonts.append(Font("\\'cb\\'ce\\'cc\\'e5", 'modern', 134, 30)) # section = Section() # document.Sections.append(section) # tps = TextPS(font=getattr(style.Fonts, "\\'cb\\'ce\\'cc\\'e5")) for i in range(self.page_num): now_page = self.pages[i] for a in now_page: for b in a: document.add_paragraph(b.decode()) if len(a)>1: # pic_num = len(self.img[a[0]]) if self.img.has_key(a[0]): for k in self.img[a[0].decode()]: pic_name = self.getImg(k) document.add_picture(self.dirname + '/' + pic_name) document.add_paragraph('---------------------------------') name = self.url.strip().split('/') name = name[-2] + name[-1] document.save(self.dirname + '/' + name + '.docx') print "Success to dump into " + name + '.docx'
def _compile_doc(self, template_path, fields_data, doc_name): """ Разархивация docx файла, преобразование и обратная архивация. """ doc = Document(template_path) doc.save(doc_name) docx_path = doc_name zip_path = docx_path.replace('docx', 'zip') folder_path = docx_path.replace('.docx', '') internal_path_xml = '/word/document.xml' internal_path_txt = '/word/document.txt' # docx > zip os.rename(docx_path, zip_path) # unzip zipfile.ZipFile(zip_path).extractall(path=folder_path) # remove initial zip os.remove(zip_path) # xml > txt os.rename(folder_path + internal_path_xml, folder_path + internal_path_txt) # replace_tags self._replace_tags(folder_path, internal_path_txt, fields_data) # txt > xml os.rename(folder_path + internal_path_txt, folder_path + internal_path_xml) # zip shutil.make_archive(folder_path, 'zip', folder_path) # remove folder shutil.rmtree(folder_path) # zip > docx os.rename(zip_path, docx_path)
def generate_docx(json_data): document = Document("static/template.docx") document.add_heading(json_data["basics"]["name"], 0) document.add_paragraph(json_data["basics"]["email"] + ' | ' + json_data["basics"]["location"] + ' | ' + json_data["basics"]["phone"] + ' | ' + 'Generated on: ' + time.strftime("%a, %d %b %Y %H:%M:%S AEST")) document.add_heading('Education', level=1) for s in json_data["education"]: document.add_paragraph(s["name"] + " - Obtained: " + s["date"] , style='ListBullet') document.add_heading('Skills', level=1) for s in json_data["skills"]: document.add_heading(s["type"], level=2) for dat in s["keywords"]: document.add_paragraph(dat, style='ListBullet') document.add_heading("Experience", level=1) for s in json_data["work"]: document.add_paragraph(s["company"] + " | " + s["position"] + " | from " + s["startDate"] + " to " + s["endDate"]) document.add_paragraph(s["summary"]) document.add_heading("Highlights", level=2) for x in s["highlights"]: document.add_paragraph(x, style='ListBullet') document.save("static/resume.docx") return document
def save2Word(): file_name = './word/'+title+'.docx' with open('./txt/mainContent.txt','a') as f1: with open('./txt/comment.txt','rb') as f2: f1.write(f2.read()) with open('./txt/mainContent.txt','rb') as article_complete: document = Document() for line in article_complete: if len(line) == 7 or len(line) == 13 or line == '\n':continue #手动换行符暂时解决不了 img_url = re.findall(r'(http:.*?\.jpg|http:.*?\.png|http:.*?\.gif)',line,re.S) line = re.sub(r'(http:.*?\.jpg|http:.*?\.png|http:.*?\.gif)','',line) for img in img_url: imgName = saveImg(img) if imgName != 0: try: document.add_picture('./img/'+imgName, width=Inches(3.25)) except Exception: document.add_paragraph(u'图片加载失败....') document.add_paragraph(img) else: document.add_paragraph(u'图片加载失败....') document.add_paragraph(img) document.add_paragraph(line.decode('UTF-8')) document.save(file_name.decode('UTF-8'))
def convert_pdf(path='provide path here', format='text', codec='utf-8'): rsrcmgr = PDFResourceManager() retstr = BytesIO() laparams = LAParams() if format == 'text': device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams) else: raise ValueError('Please provide the format to extract') fp = open(path, 'rb') interpreter = PDFPageInterpreter(rsrcmgr, device) maxpages = 500 #mention the maximum pages here (Note: Large number of pages will decrease the performance.) caching = True page_numbers=set() for page in PDFPage.get_pages(fp, page_numbers, maxpages=maxpages,caching=caching, check_extractable=True): interpreter.process_page(page) text = retstr.getvalue().decode() fp.close() device.close() retstr.close() bulletins_data = re.findall('•([^•]+)*', str(text)) list_of_bullet_points = [] json_dict = {} for points in bulletins_data: list_of_bullet_points.append(points) json_dict['bulletins'] = list_of_bullet_points json_data= json.dumps(json_dict) parsed = json.loads(json_data) final_data = json.dumps(parsed, indent=4, sort_keys=True) #creates a pretty json with the data extracted document = Document() # creates a new document document.add_heading('Bulletins data in the PDF') document.add_paragraph(str(final_data)) document.save('json_data.docx') # saves it to the filesystem os.startfile("json_data.docx") # will open the file return ''
def parse_docx(in_file, out_file): doc = Document(in_file) for item in doc.paragraphs: # 通过段落解析内容. print item.text doc.save(out_file)
def OnButton4Button(self, event): path = os.getcwd() vs.tr1300.SCPI.MMEMory.STORe.IMAGe = path + '\\temp.png' document = Document() document.add_heading('TEST REPORT',0) document.add_heading( u'品名' , level=1) document.add_paragraph(self.textCtrl1.Value, style='IntenseQuote') document.add_heading(u'型號', level=1) document.add_paragraph( self.textCtrl2.Value, style='IntenseQuote') document.add_picture(path + '\\temp.png', width=Inches(6)) a = time.localtime() s = '%04d/%02d/%02d %02d:%02d:%02d' % (a[0],a[1],a[2],a[3],a[4],a[5]) document.add_paragraph(s) t = path + '\\%04d%02d%02d_%02d%02d%02d.docx' % (a[0],a[1],a[2],a[3],a[4],a[5]) document.add_paragraph('Test by ' + self.choice1.GetString(self.choice1.GetSelection())) document.save(t) vs.tm += 1 if vs.tm == 10: wx.MessageBox('The Demo program only can run 10 times.','Up to limit!', style = wx.CENTER | wx.ICON_WARNING | wx.OK) self.Close() self.Destroy() event.Skip()
class HouseKeeping: def __init__(self, feedback_doc_name, marking_sheet_name, marker_name): self.feedback_doc_name = feedback_doc_name self.marker_name = marker_name #load student feedback form as a template self.feeback_document = Document(feedback_doc_name) #load my marking sheet 'PT' from workbook self.marking_sheet = xlrd.open_workbook(marking_sheet_name).sheet_by_name(marker_name) #username to firstname lastname map/dictionary self.name_map = {} self.construct_name_map() self.create_new_feedback_document() #probably won't work for Windows def unzip_submission(self, student_dir): #form unzip command cmd = 'unzip -d ' + student_dir + '/ ' + student_dir + '/*.zip' print cmd, '\n\n' sys_process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) sys_process.wait() std_out = sys_process.stdout.read().strip() print std_out def create_new_feedback_document(self): marker_directory = os.path.dirname(os.path.realpath(__file__))+'/'+self.marker_name for student_dir, _, file in os.walk(marker_directory): student_dir_name = os.path.relpath(student_dir, marker_directory) #print student_dir if student_dir_name is not '.': student_name = self.name_map[student_dir_name][0] + ' ' + \ self.name_map[student_dir_name][1] self.write_student_name_to_document(student_dir, student_dir_name, student_name) #just do something extra self.unzip_submission(student_dir) def write_student_name_to_document(self, student_dir, student_dir_name, student_name): #default cell for student's firstname lastname filename = self.feedback_doc_name.replace('username', student_dir_name) self.feeback_document.tables[0].cell(1,0).text = student_name self.feeback_document.save(student_dir+'/'+filename) #print student_dir+'/'+filename def construct_name_map(self): username_index = 0 is_constructing_name_map = False for i in range(self.marking_sheet.nrows): if is_constructing_name_map: username = self.marking_sheet.row_values(i)[username_index] firstname = self.marking_sheet.row_values(i)[username_index-1] lastname = self.marking_sheet.row_values(i)[username_index-2] self.name_map[username]=[firstname, lastname] elif self.marking_sheet.row_values(i).count('Username') is 1: username_index = self.marking_sheet.row_values(i).index('Username') is_constructing_name_map = True
def _save_results(filename, abstracts): document = Document() for title, content in abstracts: document.add_heading(title, level=0) for p in content.split("\r\n"): document.add_paragraph(p) document.add_page_break() document.save("./result/" + filename)
def create(title): global DOC_NAME DOC_NAME = title document = Document() document.add_heading(DOC_NAME, 0) document.save('%s.docx' % DOC_NAME) return document
def createPatentTable(patentList, docFileName): document = Document() for section in document.sections: section.orientation = WD_ORIENT.LANDSCAPE table = document.add_table(rows=1, cols=5) fillInPatentHeader(table) fillInPatentListData(table, patentList) document.save(docFileName)
def get_project_as_word(project): """ The function will retun project info in word format @Author : Arun Gopi @date : 10/4/2016 """ document = Document() ''' Setting the top margin ''' sections = document.sections section = sections[0] section.top_margin = Inches(0.1) section.left_margin = Inches(1) section.right_margin = Inches(1) errFound = False mailItenary = False dict = {} filename = '' col1_width = 2 col2_width = 15 context_dict = {} ''' printing important note ''' font = document.add_paragraph().add_run(project.title).font font.size = Pt(10) font.underline = True font = document.add_paragraph(style='ListBullet').add_run(project.short_description).font font.size = Pt(8) font = document.add_paragraph(style='ListBullet').add_run(project.detailed_description).font font.size = Pt(8) font = document.add_paragraph(style='ListBullet').add_run('Start Date : '+str(project.start_date.strftime('%d-%m-%Y'))).font font.size = Pt(8) font = document.add_paragraph(style='ListBullet').add_run('End Date : '+str(project.end_date.strftime('%d-%m-%Y'))).font font.size = Pt(8) font = document.add_paragraph(style='ListBullet').add_run('Project Manager : '+ project.manager.first_name).font font.size = Pt(8) font = document.add_paragraph(style='ListBullet').add_run('Client Name : '+project.client_name).font font.size = Pt(8) # font = document.add_paragraph(style='ListBullet').add_run(project.createdby.first_name).font # font.size = Pt(8) # font = document.add_paragraph(style='ListBullet').add_run(project.modifiedby.first_name).font # font.size = Pt(8) filename = 'project-details' filename = filename + '.docx' filename = "-".join( filename.split()) response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document') response['Content-Disposition'] = 'attachment; filename=' + filename document.save(response) return response
def get(name): saveFile = os.getcwd()+"/"+name try: doc = Document() doc.add_heading("HEAD".decode('gbk'),0) doc.add_paragraph("HEAD CONTENT".decode('gbk')) doc.save(saveFile) except : print("error")
#该脚本主要用于doc文档利用百度搜索引擎自动查寻并返回类似文本进行高亮批注 #只能读取[.docx]文件,不能读取[.doc]文件 from docx import Document #此处在r'xxxx'中输入绝对路径 path = r'C:\Users\admin\Desktop\未完整版:美腾科技所处行业分析报告(菁亿投顾-2020-05-25).docx' def get_docx_paragraph(path): paragraph = [] document = Document(path) for i in document.paragraphs: paragraph.append(i.text) #有陷阱,空list是nonetype,nonetype不能append所以不能用赋值的方法 paragraph = [i for i in paragraph if i != ''] #列表生成式去空集比较简单 return paragraph #重新输出文本 paragraph = get_docx_paragraph(path) new_doc = Document() for i in paragraph: new_doc.add_paragraph(i) new_doc.save(r'C:\Users\admin\Desktop\test.docx')
from docx import Document from docx.shared import Pt from docx.shared import RGBColor RUSure = raw_input( "do you really want to waste loads of time making the stupid file? (yes or no)" ) if RUSure == "yes" or RUSure == "Yes": doc = Document() for r in xrange(0, 64): paragraph = doc.add_paragraph() for g in xrange(0, 64): for b in xrange(0, 64): if (b % 2 == 0): text = "ur mom gay" else: text = "no u" run = paragraph.add_run(" \n " + text) font = run.font font.color.rgb = RGBColor(r * 4, g * 4, b * 4) print "done with " + str(r) + " " + str(g) doc.save('gay.docx')
p.add_run(company + ' ').bold = True p.add_run(from_date + '-' + to_date + '\n').italic = True experience_details = input('Decride you experience at ' + company) p.add_run(experience_details) else: break #skill document.add_heading('skills') skill = input('Enter skill') p = document.add_paragraph(skill) while True: has_more_skill = input('dp you has more skill? yes or no') if has_more_skill.lower() == 'yes': skill = input('Enter skill') p = document.add_paragraph(skill) p.style = 'list Bullet' else: break #footer section = document.sections[0] footer = section.footer p = footer.paragraphs[0] p.text = 'CV generated using amigoscode' document.save('cv.docx')
#%% import pathlib import os from docx import Document #%% pathlib.Path('certificates').mkdir(parents=True, exist_ok=True) #%% fname = pathlib.Path("names.txt") #%% with open(fname) as f: name_list = f.readlines() #%% for name in name_list: name = name.strip() document = Document() document.add_heading('Certificate of Participation', 0) document.add_heading('This acknowledges:', level=1) document.add_paragraph() second_paragraph = document.add_paragraph() first_run = second_paragraph.add_run(f'{name}') first_run.bold = True document.add_heading('tried the awesome automation examples', level=1) document.save(pathlib.Path(f'certificates/Certificate-{name}.docx'))
begin = datetime.strptime('%s' % begin, '%Y-%m-%d') end = datetime.now() b = begin.date() e = end.date() for i in range(len(document.paragraphs)): if len(document.paragraphs[i].text.replace(' ', '')) > 4: print("第" + str(i) + "段的内容是:" + document.paragraphs[i].text) par = input("请输入日期所在的段落号,注意请先确认日期处为空(2020年 月 日),否则日期无法自动修改") par = int(par) for i in range((e - b).days + 1): myday = b + timedelta(days=i) document = Document(r"%s" % addressIn) paragraphs = document.paragraphs text = re.sub(" 月 ", "%d月%d" % (myday.month, myday.day), paragraphs[par].text) paragraphs[par].text = text if myday.day < 10: saveadd = addressOut + "\\" + save_name + "%d月%d日.docx" % (myday.month, myday.day) else: saveadd = addressOut + "\\" + save_name + "%d月%d日.docx" % (myday.month, myday.day) date_touple = random_time() os.system("date 2020/%d/%d && time %d:%d:%d" % (myday.month, myday.day, date_touple.tm_hour, date_touple.tm_min, date_touple.tm_sec)) document.save("%s" % saveadd) print("程序执行完毕,记得重新调整系统时间哦")
class ReportBase(object): def __init__(self): self.document = Document() # style self._set_default_styles() def _set_default_styles(self): """使用默认的风格 """ self._txt_type = u'宋体' self._head_type = u'微软雅黑' cn_list = [ 'Normal', 'TOC Heading', 'Heading 1', 'Title', 'Subtitle', 'Body Text', 'Body Text 2' ] for each_cn in cn_list: set_font_styles(self.document.styles[each_cn], styles={'type': self._txt_type}) def heading(self, content, level): """添加文本标题 :param (unicode or str or list) content: list format - [[content1, style1], [content2, style2], ...] :param level: :return: Run 对象 or list """ run_ins = self.document.add_heading('', level).add_run(content) set_font_styles(run_ins, styles={'type': self._head_type}) return run_ins def text(self, content): """添加段落文本,可以输入 string 类型为直接转化为文本,也可以输入 list 分段转化为文本 :param (unicode or str or list) content: list format - [[content1, style1], [content2, style2], ...] :return: Run 对象 or list """ if isinstance(content, (str, unicode)): run_ins = self.document.add_paragraph('').add_run(content) elif isinstance(content, list): run_ins = '' else: raise Exception() return run_ins def picture(self, pic_file): """添加图片 """ # pic_file = self.file_path + os.sep + 'caigou.png' # height=Inches(3.84), width=Inches(5.76) # height=Inches(2.88), width=Inches(5.76) self.document.add_picture(pic_file, height=Inches(3.84), width=Inches(5.76)) pass def table(self, data): """添加表格 :param (list or pd.Dataframe) data: list - [[header], [value1], [value2], ...] :return: """ if hasattr(data, 'values'): columns = data.columns data = [columns] + data.values.tolist() header = data[0] contents = data[1:] col_len = len(header) table = self.document.add_table(1, col_len) table.style = 'Light List Accent 1' heading_cells = table.rows[0].cells # add heading for i in range(col_len): heading_cells[i].text = unicode(header[i]) # add content for content in contents: cells = table.add_row().cells for i, item in enumerate(content): cells[i].text = unicode(item) def save(self, save_path): """保存docx文件 """ self.document.save(save_path)
# git add . && git commit -m "Editado por Pedro" && git push --force # git add . && git push --force from docx import Document doc = Document("Covid-19.docx") # Adicionando pergunta teste doc.add_paragraph("Pergunta Teste?") doc.add_paragraph("Resposta Teste.") doc.save("Covid-19.docx")
time = date_fields[1] time_fields = time.split("_") # print(time_fields) hour = time_fields[0] minute = time_fields[1] second = time_fields[2] description = result_name + " resultado de la ejecución " + location \ + " del componente DataAccessObject para la actualización de datos en la presentación a" \ " Grupo Nutresa, generado el " + day + " de " + month + " de " + year + " a las " \ + hour + ":" + minute + ":" + second + "." else: print('Docs') name_fields = name.split(".") doc_type = name_fields[len(name_fields) - 1] print(doc_type) print(name) if doc_type == "PNG" or doc_type == "png": description = name + ". Versión del " + day + " de " + month + " de " + year + "." elif doc_type == "py": description = "Desarrollo: " + name + ". Versión del " + day + " de " + month + " de " + year + "." else: description = "Documento: " + name + ". Versión del " + day + " de " + month + " de " + year + "." print(description) p = document.add_paragraph() run = p.add_run(filename + ": ") run.bold = True run.underline = True p.add_run(description) document.save('Report - Dic - 2017.docx')
'Do you have any more skills? Type yes, if you do. And type no, if not.' ) has_more_skills = input('Any more skills? Yes or No: ') if has_more_skills.lower() == 'yes': p = document.add_paragraph() speak('What Skill do you have?') skill = input('Skill: ') speak('How much experience do you have?') amount_of_exp = input('Experience: ') p.add_run('\u2022 ' + skill + ': ').bold = True p.add_run(amount_of_exp) else: break # Footer section = document.sections[0] footer = section.footer p = footer.paragraphs[0] p.text = "Resume Generated with my great Skills" speak( 'Thank You for your Input. I will go ahead and create your Resume. Goodbye.' ) document.save('Resume.docx') # Saves All Changes to the Word File
hdr_cells21[0].add_paragraph( '\n\n院系领导意见').alignment = WD_ALIGN_PARAGRAPH.CENTER hdr_cells21[3].add_paragraph( '\n\n领导签字:' + '\n\n\t\t年\t月\t日').alignment = WD_ALIGN_PARAGRAPH.CENTER hdr_cells27[0].add_paragraph('\n\n学生处意见').alignment = WD_ALIGN_PARAGRAPH.CENTER hdr_cells27[3].add_paragraph( '\n\n领导签字:' + '\n\n\t\t年\t月\t日').alignment = WD_ALIGN_PARAGRAPH.CENTER hdr_cells32[0].add_paragraph( '\n\n主管校领导审批').alignment = WD_ALIGN_PARAGRAPH.CENTER hdr_cells32[3].add_paragraph( '\n\n领导签字:' + '\n\n\t\t年\t月\t日').alignment = WD_ALIGN_PARAGRAPH.CENTER document.save(name + 'XX申请表.docx') document1 = Document() document1.styles['Normal'].font.name = u'宋体' document1.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体') p1 = document1.add_paragraph() p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER p1.paragraph_format.line_spacing = Pt(25) r1 = p1.add_run('X X X X X X X X\rX X 清 单\n') r1.font.size = Pt(16) r1.bold = True leave_name = document1.add_paragraph('各部门:\n\n\t') leave_name.add_run(departments).font.underline = True leave_name.add_run('院(系)') leave_name.add_run(specialty).font.underline = True
temp_str = "" for x in temp_content: temp_str = temp_str + str(x) + '\n' contents_param.append(temp_str) reContent = re.compile(r'<p>(.*?)</p>') selectContent(reContent, links, contents) # 把所有的数据写入到一个文件中 articles = [] for title, description, content in zip(titles, descriptions, contents): temp = { "title": "标题:" + title, "description": description, "content": content } articles.append(temp) document = Document() document.styles['Normal'].font.name = u'宋体' document.add_heading(document_title, 0) for article in articles: document.add_heading(article.get('title'), 1) document.add_heading(article.get('description'), 2) document.add_paragraph(article.get('content')) # 获取当前时间 now = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S') document.save('C:/Users/10947/Desktop/美文' + now + '.docx')
return doc document = Document('.\\contract.docx') wb = xlrd.open_workbook('.\\input_to_contract.xlsx') sheet = wb.sheet_by_index(0) i=0 for i in range(1,sheet.nrows): old_text=str(sheet.cell_value(i, 0)) new_text=str(sheet.cell_value(i, 1)) print ("Replacing "+ old_text +" with "+new_text) #for para in document.paragraphs: #print (para.text) document=replace_string(document,old_text,new_text) # saving the file now as docx document.save('.\\output\\contract-filled.docx') #saving file as pdf now wdFormatPDF = 17 in_file = os.path.abspath('.\\output\\contract-filled.docx') out_file = os.path.abspath('.\\output\\contract-filled.pdf') word = win32com.client.Dispatch('Word.Application') doc = word.Documents.Open(in_file) doc.SaveAs(out_file, FileFormat=wdFormatPDF) doc.Close() word.Quit()
from docx import Document # creating a document from docx.shared import Inches # controlling the image size document = Document() document.add_picture( # add picture 'jqz2YF0.jpg', width=Inches(2.0)) name = input('what is your name?') # varibles phone = input('what is your phone number?') # varibles email = input('what is your email?') # varibles # putting varibles inside a paragraph document.add_paragraph(name + "|" + phone + "|" + email + "|") document.add_heading('reading') # add heading # add paragraph in a varibles p = document.add_paragraph() university = input("what university did you go to?") coursenum = input("how many courses did you take?") totalfees = input("how much did you pay?") p.add_run(university + " ").bold = True p.add_run(coursenum + " " + totalfees).italic = True your_knowledge = input("what knowledge doyou have from your" + university) p.add_run() document.save("cv.docx") # closing the document
def main(prog_name='程序名称(sql_to_csv)'): path_dir='../../../project_data/programs_json/' file_name=path_dir+prog_name+'.json' design_file = open(file_name, encoding='utf-8') # 打开详细设计的json文件 new_design_file = json.load(design_file) a = str(new_design_file) ret = re.findall(r'Table--[\u4e00-\u9fa5]+\((.*?)\)', a) # 提取table下面的表名 path = r'../../../project_data/tables_json/' # 数据库所在文件位置 files = os.listdir(path) # 打开数据库所有目录列表 data = [] for name in ret: # 遍历表名 new_name = name + '.json' # 将表名构造成json文件格式 if new_name in files: # 判断如果详细设计的表名和数据库的表名相等 data_file = open(path+new_name, encoding='utf-8') # 打开名字相等的json文件 new_data_file = json.load(data_file) data.append(new_data_file) for m,table_name in enumerate(ret): table_dict=get_table_dict(table_name,new_design_file,[])[0] """取出操作字段、条件字段、排序字段、列名这些字段的key和value""" info1=get_target_value('操作字段',table_dict,[])[0] info2=get_target_value('条件字段',table_dict,[])[0] info3=get_target_value('排序字段',table_dict,[])[0] col_num=get_target_value('序号',table_dict,[])[0] # 序号 do_type=get_target_value('操作类型',table_dict,[])[0] # 操作类型 remark=get_target_value('备注',table_dict,[])[0] # 备注 data_info=' '.join(list(get_target_value('列名',data[m],[])[0].values())) #print(table_dict) # print(info1) f = Document() # 创建table写入 f.add_paragraph('表名: '+ table_name) table = f.add_table(len(col_num)+1, 6) for i in range(len(col_num)): print('表名:'+table_name) # print('操作字段:'+info1['操作字段' + str(i)]) # print('条件字段:'+info2['条件字段' + str(i)]) # print('排序字段:'+info3['排序字段' + str(i)]) # print('data_info:'+data_info) """各字段与数据库表进行对比""" oper_str=(info1['操作字段' + str(i)]).split(' ') print(oper_str) oper_result=check_str(oper_str,data_info) print(oper_result) where_str=(info2['条件字段' + str(i)]).split(' ') print(where_str) where_result=check_str(where_str,data_info) print(where_result) order_str=(info3['排序字段' + str(i)]).split(' ') print(order_str) order_result=check_str(order_str,data_info) print(order_result) # if err_count=oper_result.count(1) + where_result.count(1) + order_result.count(1): # table = table.add_row(1) for int_ in range(len(oper_result)): if oper_result[int_] == 1: cell=table.cell(i+1,2) cell.add_paragraph(oper_str[int_]).add_run().font.color.rgb =RGBColor(0xff, 0x00, 0x00) for int_ in range(len(where_result)): if where_result[int_] == 1: cell=table.cell(i+1,3) cell.add_paragraph(where_str[int_]).add_run().font.color.rgb =RGBColor(0xff, 0x00, 0x00) for int_ in range(len(order_result)): if order_result[int_] == 1: cell=table.cell(i+1,4) cell.add_paragraph(order_str[int_]).add_run().font.color.rgb =RGBColor(0xff, 0x00, 0x00) # print(ret1,ret2,ret3) # str_he1='' # for m in range(len(error_val)): # str_he1=str_he1+'\n'+error_val[m] # table.cell(1,2).paragraphs[0].add_run(str_he1).font.color.rgb =RGBColor(0xff, 0x00, 0x00) f.save('check.docx')
from docx import Document import sys coverLetter = Document("/Users/shrav/Downloads/Cover Letters/Cover Letter Template.docx") sections = coverLetter.sections name = 'ShravanHariharan' docType = 'CoverLetter.docx' company = sys.argv[1] desc = sys.argv[2] #being one of the % out there for paragraph in coverLetter.paragraphs: if '_' in paragraph.text: inline = paragraph.runs for i in range(len(inline)): if '_' in inline[i].text: text = inline[i].text.replace('_', company) inline[i].text = text if '%' in paragraph.text: inline = paragraph.runs for i in range(len(inline)): if '%' in inline[i].text: text = inline[i].text.replace('%', desc) inline[i].text = text filename = name + '_' + company + '_' + docType filepath = "/Users/shrav/Downloads/Cover Letters/" coverLetter.save(filepath + filename)
def export(building=None, filename=None): if not building: return None X = building.x_system Y = building.y_system prop = { '': '', 'محل اجرای پروژه': building.city, 'کاربری ساختمان': 'مسکونی', 'ضریب اهمیت': building.importance_factor, 'تعداد طبقات': building.number_of_story, 'ارتفاع ساختمان )متر(': building.height, 'سطح خطر نسبی': building.risk_level, 'شتاب مبنای طرح': building.acc, 'نوع خاک': building.soilType } if X == Y: prop['سیستم سازه ای در دو راستا'] = X.lateralType else: prop['سیستم سازه ای در راستای x'] = X.lateralType prop['سیستم سازه ای در راستای y'] = Y.lateralType struc = { '': ('راستای x', 'راستای y'), 'سیستم سازه': (X.lateralType, Y.lateralType), 'ضریب رفتار': (X.Ru, Y.Ru), 'ضریب اضافه مقاومت': (X.phi0, Y.phi0), 'ضریب بزرگنمایی جابجایی': (X.cd, Y.cd), 'ارتفاع مجاز )متر(': (X.maxHeight, Y.maxHeight) } result = { 'زمان تناوب تجربی': (building.exp_period_x, building.exp_period_y), 'زمان تناوب تحلیلی': (building.x_period_an, building.y_period_an), 'ضریب بازتاب': (building.Bx, building.By), 'C': (building.results[1], building.results[2]), 'K': (building.kx, building.ky), 'C_drift': (building.results_drift[1], building.results_drift[2]), 'K_drift': (building.kx_drift, building.ky_drift), } doc = Document(os.path.join(cfactor_path, 'exporter', 'template.docx')) doc.add_heading('محاسبه ضریب زلزله', level=0) doc.add_heading('مشخصات پروژه', level=1) table_prop = doc.add_table(rows=0, cols=2, style=doc.styles['List Table 4 Accent 5']) table_prop.direction = WD_TABLE_DIRECTION.RTL for key, value in prop.items(): row_cells = table_prop.add_row().cells row_cells[0].text = key row_cells[1].text = str(value) doc.add_heading('مشخصات سیستم سازه ای', level=1) struc_table = doc.add_table(rows=0, cols=3, style=doc.styles['List Table 4 Accent 5']) for key, value in struc.items(): row_cells = struc_table.add_row().cells row_cells[0].text = key row_cells[1].text = str(value[0]) row_cells[2].text = str(value[1]) doc.add_heading('ضریب زلزله', level=1) result_table = doc.add_table(rows=1, cols=3, style=doc.styles['List Table 4 Accent 5']) hdr_cells = result_table.rows[0].cells hdr_cells[1].text = 'راستای x' hdr_cells[2].text = 'راستای y' for key, value in result.items(): row_cells = result_table.add_row().cells row_cells[0].text = key row_cells[1].text = f'{value[0]:.3f}' row_cells[2].text = f'{value[1]:.3f}' if filename: doc.save(filename)
lines = data['sources'] for l in range( filtered_issues[values[v]][i]['textRange']['startLine'] - 1, filtered_issues[values[v]][i]['textRange']['endLine']): print(lines[l]) p = document.add_paragraph() code = lines[l][1] code = clean_code(code) p.add_run("{0}: {1}".format(lines[l][0], code)).bold = True ############## Mitigations ############## p = document.add_heading('Possible mitigations', level=1) p.paragraph_format.page_break_before = True for phase in vulnerabilities[values[v]]['Possible mitigations']: document.add_paragraph() p = document.add_paragraph() p.add_run(phase).bold = True for mitigation in vulnerabilities[ values[v]]['Possible mitigations'][phase]: p = document.add_paragraph() p.add_run(mitigation) ############## Links ################ document.add_heading('Reference links', level=1) document.add_paragraph() for link in vulnerabilities[values[v]]['Reference links']: p = document.add_paragraph() p.add_run(link).bold = True document.save('Analysis report.docx')
def extract_word_file(docs, weights, ori_qas, answers, confidents, fname): # with open(os.path.join(tmp_path, fname), 'w') as fw: document = Document() p = document.add_paragraph('注: 信息关联度(从强到弱)为`红->橙->棕->绿->黑`\n\n\n') count = 1 for doc, weight, ori_qa, answer, conf in zip(docs, weights, ori_qas, answers, confidents): weight = [_ for i, _ in enumerate(weight) if doc[i] != '' and doc[i] != ' '] doc = [_ for _ in doc if _ != '' and _ != ' '] run = p.add_run('题目{}:\n\n'.format(count)) # weight = ' '.join(list(map(str, weight))) conf = str(conf) # print(doc) # print(weight) # print(ori_qa) # print(answer) # print(conf) #add_run在同一段添加内容 run = p.add_run('文章:\n') doc = '#####'.join(doc) doc_sent = cut_sent(doc) trans_doc_sent = translate('\n'.join([''.join(_.split('#####')) for _ in doc_sent])) w_i = 0 total_sent = len(doc_sent) colors = [] for sent in doc_sent: sent = sent.split('#####') total_score = 0. for w in sent: if not w: continue if w_i >= len(weight): break wei= weight[w_i] w_i += 1 if wei > 10. / len(weight): total_score += 8 elif wei > 6. / len(weight): total_score += 6 elif wei > 3. / len(weight): total_score += 4 elif wei > 1.5 / len(weight): total_score += 2 if total_score >= 14: color = RGBColor(255,0,0) elif total_score >= 10: color = RGBColor(255,165,0) elif total_score >= 8: color = RGBColor(165,42,42) elif total_score >= 6: color = RGBColor(0,128,0) else: color = RGBColor(0,0,0) colors.append(color) for w in sent: run = p.add_run(w) run.font.color.rgb = color run = p.add_run('\n翻译:\n') for _, trans_sent in enumerate(trans_doc_sent): for w in trans_sent: run = p.add_run(w) run.font.color.rgb = colors[_] # for w, wei in zip(doc, weight): # if w == '\n': # continue # if wei > 10. / len(weight): # run.font.color.rgb = RGBColor(255,0,0) # elif wei > 6. / len(weight): # run.font.color.rgb = RGBColor(255,165,0) # elif wei > 3. / len(weight): # run.font.color.rgb = RGBColor(165,42,42) # elif wei > 1.5 / len(weight): # run.font.color.rgb = RGBColor(0,128,0) # else: # run.font.color.rgb = RGBColor(0,0,0) # doc = ' '.join(doc) # doc = doc.replace(' \n ', '\n') # run = p.add_run('文章:\n' + doc + '\n\n') run = p.add_run('\n\n问题:\n' + ori_qa + '\n\n') run = p.add_run("最终答案: " + answer + '\n\n') run = p.add_run('置信度: ' + conf) run = p.add_run('\n' * 5) count += 1 # fw.write('\n\n'.join(['注:信息关联度(从强到弱):红->橙->黄->黑\n', '文章:\n' + doc, weight, '问题:\n' + ori_qa, "最终答案:" + answer, '置信度:' + conf]) + '\n' * 5) document.save(os.path.join(tmp_path, fname)) with open(os.path.join(tmp_path, fname), 'rb') as f: output = bucket.put_object( fname, body=f ) if output.status_code == 201: print('Upload Success') else: print('Upload Failed') os.remove(os.path.join(tmp_path, fname))
# pip3 install python-docx from docx import Document import re file_path = '/home/alex/WEB/Contrato.docx' regex1 = re.compile(r'2019') replace1 = r'2018' def docx_replace_regex(doc_obj, regex, replace): for p in doc_obj.paragraphs: if regex.search(p.text): inline = p.runs for i in range(len(inline)): if regex.search(inline[i].text): print(inline[i].text) inline[i].underline = True inline[i].text = replace for table in doc_obj.tables: for row in table.rows: for cell in row.cells: docx_replace_regex(cell, regex, replace) document = Document(file_path) docx_replace_regex(document, regex1, replace1) document.save('xdxdxd.docx')
def data_sheet(request, id): idprefix = request.POST['idprefix'] print(idprefix, 'jjjjjjjjjjjj') doc_final_path = 'E:/certa-drdo/certa/TA_Datasheet.docx' final_path = 'E:/certa-drdo/certa/' # finalpath=final_path.replace('/','\\') pdf_final_path = 'E:/certa-drdo/certa/TA_Datasheet.pdf' if os.path.isfile(pdf_final_path): with open(pdf_final_path, 'rb') as pdf: response = HttpResponse(pdf.read(), content_type='application/pdf') response['Content-Disposition'] = 'filename=some_file.pdf' return response elif os.path.isfile(doc_final_path): print('mmmmmmmmmmmmmm') pythoncom.CoInitialize() wdFormatPDF = 17 # print(tempfile.gettempdir(),'temp') in_file = os.path.abspath(doc_final_path) # out_file = os.path.abspath('D:/cemilac/certa/defence/media/org1.pdf') word = comtypes.client.CreateObject('Word.Application') doc = word.Documents.Open(in_file) doc.SaveAs('E:/certa-drdo/certa/TA_Datasheet.pdf', FileFormat=wdFormatPDF) print('nnnnnnnnnnn') doc.Close() word.Quit() with open(final_path + 'TA_Datasheet.pdf', 'rb') as pdf: response = HttpResponse(pdf.read(), content_type='application/pdf') response['Content-Disposition'] = 'filename=some_file.pdf' return response else: curr_path = "/" + str(id) + "/" + idprefix + "Annexure 6/" curr_path = curr_path.replace('/', '\\') new_path = os.path.join(settings.MEDIA_ROOT + curr_path) # if os.path.isdir(new_path): # with open(new_path+'TA Datasheet.docx', 'rb') as pdf: # response = HttpResponse(pdf.read(),content_type='application/pdf') # response['Content-Disposition'] = 'filename=some_file.pdf' # return response # else: taa = TAapplicationmodel.objects.filter(user_id=id).first() # template = get_template('dealing officer/Draft TA pdf.html') target_file = StringIO() template = DocxTemplate( "E:/certa-drdo/certa/dashboard/templates/dealing officer/DS template.docx" ) context = { 'firmname': taa.firmname, 'addr1': taa.addr1, 'item_name': taa.item_name, 'part_no': taa.part_no } html = template.render(context) doc_io = io.BytesIO() # create a file-like object template.save("TA_Datasheet.docx") # save data to file-like object new_path1 = 'E:\certa-drdo\certa\TA_Datasheet.docx' # output_path = os.path.join(settings.MEDIA_ROOT) + '/89/result.pdf' # new_path=new_path.replace('\','//') taa = TAapplicationfiles.objects.filter(user_id=id, refid=idprefix, refpath='Annexure 6').first() aesurl = taa.filepath docurl = aesurl[:-4] print('aesview', aesurl) print('docurl', docurl) bufferSize = 64 * 1024 passw = "#EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e" encFileSize = stat(aesurl).st_size with open(aesurl, "rb") as fIn: with open(docurl, "wb") as fOut: pyAesCrypt.decryptStream(fIn, fOut, passw, bufferSize, encFileSize) templateDoc1 = Document(new_path1) templateDoc = Document(docurl) # templateDoc1.add_page_break() for element in templateDoc.element.body: templateDoc1.element.body.append(element) templateDoc1.save(new_path1) messages.success( request, 'Data_sheet Successfully Prepared, Click again to view the file !') reg = TAapplicationmodel.objects.filter( file_in_id=str(request.user.id)) return render(request, 'tcs do/receivedtyperecord.html', { 'details': reg, 'status': True })
files = os.listdir(path) #得到文件夹下的所有文件名称/ # print(path) # print(files) # doSaveAas(path,files[0]) s = [] titles = [] for file in files: #遍历文件夹 if not os.path.isdir(file): #判断是否是文件夹,不是文件夹才打开 if ".docx" in file: document = Document(file) #打开文件 *.docx str = "" for paragraph in document.paragraphs: str = str + paragraph.text # 打印各段落内容文本 document.save(file) # 保存文档 titles.append(file) s.append(str) # 每个文件的文本存到list中 final_list = list(zip(titles, s)) # print(final_list) #打印结果 flag = True keyword = input("请输入关键词:") print("*" * 18) print() for i in final_list: if keyword in i[1]: print(i[0]) flag = False print()
table.rows[0].cells[2].text = '关系' table.rows[0].cells[2].width = Cm(15) for i, j in enumerate(rel): table.rows[i + 1].cells[0].text = 'Template' + j.replace(' ', '') table.rows[i + 1].cells[0].width = Cm(5) table.rows[i + 1].cells[1].text = j table.rows[i + 1].cells[1].width = Cm(5) table.rows[i + 1].cells[2].width = Cm(15) l_rel = rel[j] l_table = table.rows[i + 1].cells[2].add_table(len(l_rel) + 1, 3) l_table.style = style l_table.rows[0].cells[0].text = '关系名(en)' l_table.rows[0].cells[1].text = '关系名(zh)' l_table.rows[0].cells[2].text = '内部关系' for ii, jj in enumerate(l_rel): l_table.rows[ii + 1].cells[0].text = jj l_table.rows[ii + 1].cells[1].text = l_rel[jj]['zh'] if jj.__contains__('multi'): i_rel = l_rel[jj]['inner_relation'] i_table = l_table.rows[ii + 1].cells[2].add_table( len(i_rel) + 1, 2) i_table.style = style i_table.rows[0].cells[0].text = '关系名(en)' i_table.rows[0].cells[1].text = '关系名(zh)' for iii, jjj in enumerate(i_rel): i_table.rows[iii + 1].cells[0].text = jjj i_table.rows[iii + 1].cells[1].text = i_rel[jjj]['zh'] document.save('../ms_wiki_data/template_relation.docx')
noofcols = 3 noofrows = int(imglstlen / 3) addimages(imglist, 3, noofrows, noofcols) else: noofrows = int(imglstlen / 3) + 1 noofcols = 3 addimages(imglist, 3, noofrows, noofcols) #VIDEO LINKS vdolist = article.movies addline("Videos", "B", "L") if len(vdolist) > 0: for vdo in vdolist: vidoecount = vidoecount + 1 addline("Article Videos " + str(vidoecount) + ":", "B", "L") addline(vdo, "I", "L") vidoecount = 0 else: addline("No Videos for this article.", "B", "C") #SUMMARY summry = article.summary addline("Summary", "B", "L") writearticle(summry) #KEYWORDS kwrds = article.keywords writekwrds(kwrds) #SAVING DOCUMENT document.add_page_break() document.save(docname) Articlecount = Articlecount + 1 print(Articlecount, title)
else: row_cells[d].text = str(data_table_2[col_names[d]].values[i]) document.add_paragraph(text='\n') # generate stores activity per each region for region in sorted(activities['region_name'].unique()): activities_region = activities[activities['region_name'] == region] print('Activity of region %s' % region) activities_yes = activities_region[activities_region['is_active'] == 'yes'] activities_no = activities_region[activities_region['is_active'] == 'no'] document.add_paragraph(text=region) table = document.add_table(rows=1, cols=3, style='rm_simple') header_cells = table.rows[0].cells header_cells[0].text = '' header_cells[1].paragraphs[0].add_run( 'Which stores have been using the system').bold = True header_cells[2].paragraphs[0].add_run( 'Which stores have not been using the system').bold = True shade_cells([table.cell(0, 1)], "#00b050") shade_cells([table.cell(0, 2)], "#3d393a") for i in range(max([len(activities_no), len(activities_yes)])): row_cells = table.add_row().cells row_cells[0].text = str(i + 1) if i < len(activities_yes): row_cells[1].text = str(activities_yes['store_name'].values[i]) if i < len(activities_no): row_cells[2].text = str(activities_no['store_name'].values[i]) document.add_paragraph(text='\n') document.save('output/Maxbo Report Week 16.docx')
cell.merge(table.cell(d['row_end'] - 1, d['col_end'] - 1)) if not cell.text: text = j[1][0] cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER run = cell.paragraphs[0].add_run(text) run.font.size = Pt(7) cell.paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER cell.paragraphs[0].paragraph_format.space_after = Pt(0) cell.paragraphs[0].paragraph_format.line_spacing_rule = 0 cell.paragraphs[0].paragraph_format.space_before = Pt(0) except Exception as ex: print('cell_error', ex) # cell = table.cell(d['row_begin'], d['col_begin']) # if not cell.text: # text = j[1][0] return doc if __name__ == '__main__': img = Image.open(r'C:\Users\Admin\Desktop\texts.jpg') img.thumbnail((2500, 2500)) doc = Document() data = pickle.load(open(r'F:\paragraph_restore\pdf\p1\76\76_103.pkl', 'rb')) data = [i for i in data if i[0] == 'table'] for i in data: doc = restore_table(doc, i, img) doc.save('1.docx') pass
def CompositeTwoDocs(srcDocFullName, dstDocFullName, compositeName): ''' srcDocFullName:源文档,里面含有需要替换的内容 dstDocFullName:目标文档,执行后,相关模板内容被替换 compositeName:替换的对象名,比如正面或背面 return: 成功->True,失败->False ''' try: srcDoc = Document(srcDocFullName) dstDoc = Document(dstDocFullName) srcParasMap = {} # Heading 2 => [paras list] dstParasMap = {} # Heading 2 => [paras list] firstPage = False secondPage = False currentLabelStyleContent = None # 当前标签样式对应的内容 # 查找源文档的相关内容 for srcPara in srcDoc.paragraphs: if (srcPara.style.name.find('Heading 2') >= 0 and srcPara.text.find(compositeName) >= 0): print('find {0}'.format(srcPara)) firstPage = True elif (srcPara.style.name.find('Heading 2') >= 0 and firstPage): secondPage = True break else: if (firstPage and not secondPage): if (srcPara.style.name.find('Heading 3') >= 0): srcParasMap[srcPara.text] = [] currentLabelStyleContent = srcPara.text else: if currentLabelStyleContent is None: raise ValueError('不合格的word模板文档!') srcParasMap[currentLabelStyleContent].append(srcPara) firstPage = False secondPage = False currentLabelStyleContent = None # 当前标签样式对应的内容 # 查找目标文档的相关内容 for dstPara in dstDoc.paragraphs: if (dstPara.style.name.find('Heading 2') >= 0 and dstPara.text.find(compositeName) >= 0): print('find {0}'.format(dstPara)) firstPage = True elif (dstPara.style.name.find('Heading 2') >= 0 and firstPage): secondPage = True break else: if (firstPage and not secondPage): if (dstPara.style.name.find('Heading 3') >= 0): dstParasMap[dstPara.text] = [] currentLabelStyleContent = dstPara.text else: if currentLabelStyleContent is None: raise ValueError('不合格的word模板文档!') dstParasMap[currentLabelStyleContent].append(dstPara) # 开始组合 for key, dstParas in dstParasMap.items(): srcParas = srcParasMap[key] if len(srcParas) <= 0: print('源文档中没有该项--{0}--内容'.format(key)) continue else: for index, item in enumerate(dstParas): if (index <= len(srcParas)): dstParas[index].text = srcParas[index].text else: print('{0}中的长度--{1}--已经大于源文档的总长度--{2}'.format(key, index, len(srcParas))) dstDoc.save(dstDocFullName) except Exception as e: print('出现错误...') print(e) return False return True
'\nÉ um facto estabelecido de que um leitor é distraído pelo conteúdo legível de uma página quando analisa a sua mancha gráfica. Logo, o uso de Lorem Ipsum leva a uma distribuição mais ou menos normal de letras, ao contrário do uso de "Conteúdo aqui, conteúdo aqui", tornando-o texto legível. Muitas ferramentas de publicação electrónica e editores de páginas web usam actualmente o Lorem Ipsum como o modelo de texto usado por omissão, e uma pesquisa por "lorem ipsum" irá encontrar muitos websites ainda na sua infância. Várias versões têm evoluído ao longo dos anos, por vezes por acidente, por vezes propositadamente (como no caso do humor).' ) projetos = curriculo.add_paragraph('Projetos pessoais', style="Heading 1") for projeto in range(1, 3): projetos = curriculo.add_paragraph(f'Projeto {projeto}', style="Heading 2") texto = curriculo.add_paragraph( '\nExistem muitas variações das passagens do Lorem Ipsum disponíveis, mas a maior parte sofreu alterações de alguma forma, pela injecção de humor, ou de palavras aleatórias que nem sequer parecem suficientemente credíveis. Se vai usar uma passagem do Lorem Ipsum, deve ter a certeza que não contém nada de embaraçoso escondido no meio do texto.' ) experiencias = curriculo.add_paragraph('Experiência', style="Heading 1") for experiencia in range(1, 3): nome_empresa = curriculo.add_paragraph(f'NOME DA EMPRESA {projeto} \n', style="Heading 2") texto = curriculo.add_paragraph('Data: [Inicio] - [FIM]') texto = curriculo.add_paragraph('Atividades feitas', style="List Bullet") texto = curriculo.add_paragraph('Atividades feitas', style="List Bullet") texto = curriculo.add_paragraph('Atividades feitas', style="List Bullet") formacao_academica = curriculo.add_paragraph('Experiência', style="Heading 1") for formacao in range(1, 3): nome_empresa = curriculo.add_paragraph(f'NOME DA EMPRESA {projeto} \n', style="Heading 2") texto = curriculo.add_paragraph('Data: [Inicio] - [FIM]') texto = curriculo.add_paragraph('Nome do curso', style="List Bullet") texto = curriculo.add_paragraph('Descrição sobre o curso', style="List Bullet") curriculo.save('curriculoDemo.docx')
run2.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体') run2.font.size = Pt(16) run2.font.bold = True p3 = document.add_paragraph() run3 = p3.add_run('因为疫情影响,我们很抱歉的通知您,您的工资调整为每月%s元,特此通知。' % price) run3.font.name = '宋体' run3.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体') run3.font.size = Pt(14) table = document.add_table(rows=2, cols=2, style='Table Grid') # 合并单元格 table.cell(0, 0).merge(table.cell(0, 1)) table_run1 = table.cell(0, 0).paragraphs[0].add_run('签名栏') table_run1.font.name = '黑体' table_run1.element.rPr.rFonts.set(qn('w:eastAsia'), u'黑体') table.cell(0, 0).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER table.cell(1, 0).text = i table.cell(1, 0).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER p4 = document.add_paragraph() p4.alignment = WD_ALIGN_PARAGRAPH.RIGHT run4 = p4.add_run('人事:王小姐 电话:686868') run4.font.name = '宋体' run4.element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体') run4.font.size = Pt(14) run4.font.bold = True document.save('%s-工资调整通知.docx' % i)
for row in table.rows: for cell in row.cells: lst = iter(cell.paragraphs) for paragraph in lst: if subject in paragraph.text: paragraph.add_run("\n" + subjectReport, style = 'ReportStyle') for para in lst: delete_paragraph(para) return def addSubjectReport(subject, subjectReport, subjectAttainment, subjectEffort): addSubjectReportText(subject, subjectReport) addSubjectEffort(subject, subjectEffort) addSubjectAttainment(subject, subjectAttainment) return addDetails() addSubjectReport('Religious Education', religStudyReport, religStudy, religStudyEffort) addSubjectReport('English', englishReport, (english + phonics) // 2, (englishEffort + phonicsEffort) // 2) addSubjectReport('Mathematics', mathsReport, maths, mathsEffort) addSubjectReport('Science', scienceReport, science, scienceEffort) addGeneralCommentsText(generalReport) addOtherSubjectsReport(otherSubjectsReport) documentName = firstName + ' ' + lastName + '.docx' document.save(documentName) print(report)
document.add_paragraph('first item in ordered list', style='List Number') # making connection to db conn = engine.connect() # sample data records = ((3, '101', 'Spam'), (7, '422', 'Eggs'), (4, '631', 'Spam, spam, eggs, and spam')) # writing to db for qty, id, desc in records: record = records_table.insert().values(id=id, qty=qty, desc=desc) conn.execute(record) table = document.add_table(rows=1, cols=3) hdr_cells = table.rows[0].cells hdr_cells[0].text = 'Qty' hdr_cells[1].text = 'Id' hdr_cells[2].text = 'Desc' # reading all from database r = records_table.select() res = conn.execute(r) # for each row in res perform the write operation for qty, id, desc in res: row_cells = table.add_row().cells row_cells[0].text = str(qty) row_cells[1].text = id row_cells[2].text = desc document.add_page_break() document.save('demo.docx')
def createTopicTable(patentList, docFileName): document = Document() for section in document.sections: section.orientation = WD_ORIENT.LANDSCAPE table = document.add_table(rows=1, cols=len(patentList) + 1) fillInTopicData(table, patentList) document.save(docFileName)